Java时间格式Date/LocalDateTime/LocalDate/String/long(时间戳)互转

时间:2025-04-02 06:56:16

目录

  • 转Date
      • LocalDateTime转Date
      • LocalDate转Date
      • String转Date
      • long(时间戳)转Date
  • 转LocalDateTime
      • Date转LocalDateTime
      • LocalDate转LocalDateTime
      • String转LocalDateTime
      • long(时间戳)转LocalDateTime
  • 转LocalDate
      • Date转LocalDate
      • LocalDateTime转LocalDate
      • String转LocalDate
      • long(时间戳)转LocalDate
  • 转String
      • Date转String
      • LocalDateTime转String
      • LocalDate转String
      • long(时间戳)转String
  • 转long(时间戳)
      • Date转long(时间戳)
      • LocalDateTime转long(时间戳)
      • LocalDate转long(时间戳)
      • String转long(时间戳)

转Date

LocalDateTime转Date

ZoneId zone = ZoneId.systemDefault();

LocalDateTime dateTime = LocalDateTime.now();
ZonedDateTime zdf = dateTime.atZone(zone);
Date date = Date.from(zdf.toInstant());

LocalDate转Date

ZoneId zone = ZoneId.systemDefault();

LocalDate localDate = LocalDate.now();
// 实质上是把LocalDate先转成了LocalDateTime
ZonedDateTime zdf = localDate.atStartOfDay().atZone(zone);
Date date = Date.from(zdf.toInstant());

String转Date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    Date date = sdf.parse("2021-01-01 00:00:00");
} catch (ParseException e) {
    e.printStackTrace();
}

long(时间戳)转Date

long timestamp = System.currentTimeMillis();
Date date = new Date(timestamp);

转LocalDateTime

Date转LocalDateTime

ZoneId zone = ZoneId.systemDefault();

Date date = new Date();
LocalDateTime dateTime = date.toInstant().atZone(zone).toLocalDateTime();

LocalDate转LocalDateTime

LocalDate localDate = LocalDate.now();
LocalDateTime dateTime = localDate.atStartOfDay();

String转LocalDateTime

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse("2021-01-01 00:00:00", df)

long(时间戳)转LocalDateTime

ZoneId zone = ZoneId.systemDefault();

long timestamp = System.currentTimeMillis();
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zone);

转LocalDate

Date转LocalDate

ZoneId zone = ZoneId.systemDefault();

Date date = new Date();
LocalDate localDate = date.toInstant().atZone(zone).toLocalDate();

LocalDateTime转LocalDate

LocalDateTime dateTime = LocalDateTime.now();
LocalDate localDate = dateTime.toLocalDate();

String转LocalDate

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse("2021-01-01", df)

long(时间戳)转LocalDate

ZoneId zone = ZoneId.systemDefault();

long timestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(zone).toLocalDate();

转String

Date转String

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = sdf.format(date);

LocalDateTime转String

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.now();
String s = df.format(dateTime);

LocalDate转String

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now();
String s1 = df.format(localDate);
// or
String s2 = localDate.toString();

long(时间戳)转String

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timestamp = System.currentTimeMillis();
Date date = new Date(timestamp);
String s = sdf.format(date);

转long(时间戳)

Date转long(时间戳)

Date date = new Date();
long timestamp = date.getTime();

LocalDateTime转long(时间戳)

LocalDateTime dateTime = LocalDateTime.now();
long timestamp = Timestamp.valueOf(dateTime).getTime();

LocalDate转long(时间戳)

LocalDate localDate = LocalDate.now();
long timestamp = Timestamp.valueOf(localDate.atStartOfDay()).getTime();

String转long(时间戳)

String s = "2021-01-01 00:00:00";
long timestamp = Timestamp.valueOf(s).getTime();