Java时间类记录
一、前言
- 环境说明:
JDK:1.8
官方API文档:https://docs.oracle.com/javase/8/docs/api/index.html
二、记录
- java.util.Date
参考:https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println(sdf.format(date));
- java.util.Calendar
参考:https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
// int hour = calendar.get(Calendar.HOUR); // 12小时制
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(year +"-"+ month +"-"+ day +" "+ hour +":"+ minute +":"+ second);
// or
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(calendar.getTime()));
- java.time.LocalDate 、java.time.LocalTime 、java.time.LocalDateTime
JDK:1.8+
参考:
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
DateTimeFormatter dateDTF = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now();
DateTimeFormatter timeDTF = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
System.out.println(localDate.format(dateDTF) +" "+ localTime.format(timeDTF));
// or
DateTimeFormatter datetimeDTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.format(datetimeDTF));
- System.currentTimeMillis()
参考:https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#currentTimeMillis–
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
System.out.println(sdf.format(date));
- java.sql.Timestamp
参考:https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
System.out.println(sdf.format(timestamp));
// or
timestamp = new Timestamp(System.currentTimeMillis());
date = new Date(timestamp.getTime());
System.out.println(sdf.format(date));
- java.sql.Date
参考:https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println(sdf.format(sqlDate));
// or
sqlDate = new java.sql.Date(System.currentTimeMillis());
date = new Date(sqlDate.getTime());
System.out.println(sdf.format(date));
三、其它
1.获取未来或过去24小时的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); // 设置当前时间
for(int i=0; i< 24; i++){
String time = sdf.format(calendar.getTime());
System.out.println(time);
// 如果 -1 则为是过去24小时的时间
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 1);
}