publicstaticvoid main(String[]args){
DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat1.format(new Date()));
System.out.println(dateFormat1.format(Calendar.getInstance().getTime()));
DateFormat dateFormat2 = new SimpleDateFormat("yyyy年-MM月-dd日 HH:mm:ss");
System.out.println(dateFormat2.format(new Date()));
System.out.println(dateFormat2.format(Calendar.getInstance().getTime()));
}
Calendar calendar = Calendar.getInstance();
//一年前
calendar.add(Calendar.YEAR, -1);
//两个月后
calendar.add(Calendar.MONTH, 2);
Date date = new Date(1418870012469L);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(date));
calendar.setTimeInMillis(1404564546487L);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(calendar.getTime()));
字符串转Calendar:
publicstaticvoid main(String[]args) throws ParseException{
DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = dateFormat1.parse("2015-04-20 00:00:01");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = dateFormat2.parse("2015-04-20");
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
System.out.println(calendar1.getTimeInMillis());
System.out.println(calendar2.getTimeInMillis());
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
String timeStr = dateFormat.format(calendar.getTime());
Calendar转Date:
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
Date转Calendar:Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date.getTime());