Date d1 = new Date();
long date = 1000*60*60;
Date d2 = new Date(date)//1970-1-1 0:0:0 以后
getTime
setTime(long time)
Date d = new Date();
System.out.println(d.getTime());
long time = System.currentTimeMillis();
d.setTime(time);
SimpleDateFormat格式化
SimpleDateFormat()
SimpleDateFormat(String pattern)
String format(Date date)
Date pase(String soruce)
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat();
String s = sdf.format(d);
System.out.println(s);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s2 = sdf22.format(d);
System.out.println(s2);
String ss = "2000-01-01 11:11:11"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(ss);
System.out.println(d);
日期工具类
自制
public class DateUtils{
public static String dataToString(Date date,String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String s = sdf.format(date);
return s;
}
public static Date stringToDate(String s,String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(s);
return date;
}
}
Calendar
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int year = c.get(Calendar.DATE);
get(int field)
add(int field,int amount)
set(int year,int mounth,int date)
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
c.add(Calendar.YEAR,-3)//减去3年
int year2 = c.get(Calendar.YEAR);
Calendar c = Calendar.getInstance();
c.set(2048,11,11);//2048-12-11
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int year = c.get(Calendar.DATE);