final Date date = new Date();
System.out.println("date:"+date);
final Timestamp timestamp = new Timestamp(date.getTime());
System.out.println("timestamp:"+timestamp); System.out.println("date.getTime():"+date.getTime());
final Calendar calendar =Calendar.getInstance();
System.out.println("calendar:"+calendar);
final Instant instant = Instant.now();
System.out.println("instant:"+instant);
final LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDateTime:"+localDateTime);
final ZonedDateTime zonedDateTime = ZonedDateTime.now(); System.out.println("zonedDateTime:"+zonedDateTime);
输出:
date:Wed Dec 01 10:37:24 CST 2021
timestamp:2021-12-01 10:37:24.501
date.getTime():1638326244501
calendar:java.util.GregorianCalendar[time=1638326244506,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2021,MONTH=11,WEEK_OF_YEAR=49,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=335,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=37,SECOND=24,MILLISECOND=506,ZONE_OFFSET=28800000,DST_OFFSET=0]
instant:2021-12-01T02:37:24.508Z
localDateTime:2021-12-01T10:37:24.569
zonedDateTime:2021-12-01T10:37:24.570+08:00[Asia/Shanghai]
Date 转 字符串
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strTime = df.format(date);
输出:
2021-12-01 10:27:10
时间戳 转 字符串
Date date = new Date();
long time = date.getTime();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sf.format(new Date(time)));
输出:时间戳->Date->字符串
2021-12-01 10:52:38
字符串 转 时间戳
String date_str = "2021-12-01 10:52:38";
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.parse(date_str).getTime()/1000);
} catch (Exception e) {
e.printStackTrace();
}
输出:字符串->Date->时间戳
1638327158
转 Date
//Timestamp转Date
Date timestampDate = new Date(timestamp.getTime());
//Calendar转Date
Date calendarDate = calendar.getTime();
//Instant转Date
Date instantDate = Date.from(instant);
//LocalDateTime转Date
Date localDateTimeDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
//ZonedDateTime转Date
Date zonedDateTimeDate = Date.from(zonedDateTime.toInstant());
转 Instant
//Date转Instant
Instant dateInstant = date.toInstant();
//Timestamp转Instant
Instant timestampInstant = timestamp.toInstant();
//Calendar转Instant
Instant calendarInstant = calendar.toInstant();
//LocalDateTime转Instant
Instant localDateTimeInstant = localDateTime.toInstant(ZoneOffset.of(ZoneId.systemDefault().getId()));
//ZonedDateTime转Instant
Instant zonedDateTimeInstant = zonedDateTime.toInstant();
转 LocalDateTime
//Date转LocalDateTime
LocalDateTime dateLocalDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
//Timestamp转LocalDateTime
LocalDateTime timestampLocalDateTime = timestamp.toLocalDateTime();
//Calendar转LocalDateTime
LocalDateTime calendarLocalDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());
//Instant转LocalDateTime
LocalDateTime instantLocalDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
//ZonedDateTime转LocalDateTime
LocalDateTime zonedDateTimeLocalDateTime = zonedDateTime.toLocalDateTime();
转 ZonedDateTime
//Date转ZonedDateTime
ZonedDateTime dateZonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
//Timestamp转ZonedDateTime
ZonedDateTime timestampZonedDateTime = ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
//Calendar转ZonedDateTime
ZonedDateTime calendarZonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
//Instant转ZonedDateTime
ZonedDateTime instantZonedDateTime = instant.atZone(ZoneId.systemDefault());
//LocalDateTime转ZonedDateTime
ZonedDateTime localDateTimeZonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
转 Timestamp
//Date转Timestamp
Timestamp dateTimestamp = new Timestamp(date.getTime());
//Calendar转Timestamp
Timestamp calendarTimestamp = new Timestamp(calendar.getTimeInMillis());
//Instant转Timestamp
Timestamp instantTimestamp = Timestamp.from(instant);
//LocalDateTime转Timestamp
Timestamp localDateTimeTimestamp = Timestamp.valueOf(localDateTime);
//ZonedDateTime转Timestamp
Timestamp zonedDateTimeTimestamp = Timestamp.from(zonedDateTime.toInstant());
转 Calendar
//Date转Calendar
Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTime(date);
//Timestamp转Calendar
Calendar timestampCalendar = Calendar.getInstance(); timestampCalendar.setTimeInMillis(timestamp.getTime());
//Instant转Calendar
Calendar instantCalendar = GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));
//LocalDateTime转Calendar
Calendar localDateTimeCalendar = GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
//ZonedDateTime转Calendar
Calendar zonedDateTimeInstantCalendar = GregorianCalendar.from(zonedDateTime);