[java]代码库
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DateTest {
public static void main(String[] args) {
Date d = new Date();
Date nd = new Date(d.getTime()-1000*60*60*24);
System.out.println(d);
System.out.println(nd);
System.out.println(d.before(nd));
Date end = new Date();
System.out.println(end.getTime()-d.getTime());
}
}
DateFormat:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
public class DateFormatTest {
public static void main(String[] args)throws ParseException {
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;//可以有语言Locale.ENGLISH
System.out.println(format.format(d));
format.applyPattern("EEE MM,dd,yyyy HH:mm:ss ");
System.out.println(format.format(d));
format.applyPattern("MM,dd,yy HH:mm:ss");
System.out.println(format.format(d));
Date d2 = format.parse("10,31,16 19:18:56");
System.out.println(d2);
SimpleDateFormat format2 = new SimpleDateFormat("EEE MM,dd,yyyy HH:mm:ss ", Locale.ENGLISH);
System.out.println(format2.format(d));
import java.util.Calendar;
public class CalendarTest {
public static void main (String[] args){
Calendar c = Calendar.getInstance();
c.set(2016, 7, 5, 22, 12, 1);
System.out.println(c.getTime());
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(Calendar.MONDAY);
System.out.println(c.get(Calendar.DAY_OF_YEAR));
c.set(Calendar.MONTH, 8);
c.set(Calendar.DAY_OF_MONTH, 31);
System.out.println("after set date : " + c.getTime());
for (int i=0;i<50;i++) {
c.roll(Calendar.DAY_OF_MONTH, true);
}
System.out.println("after roll : " + c.getTime());
for (int i=0;i<50;i++) {
c.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println("after add 1 : " + c.getTime());
for (int i=0;i<50;i++) {
c.add(Calendar.DAY_OF_MONTH, -1);
}
System.out.println("after add -1 : " + c.getTime());
}