String、Date、LocalDate之间的转换,这一篇就够了!!!

文章目录

    • 一、Date和String
      • 1、Date 转 String
      • 2、String 转 Date
    • 二、Date和LocalDate
      • 1、Date 转 LocalDate
      • 2、LocalDate 转 Date
    • 三、LocalDate和String
      • 1、LocalDate 转 String
      • 2、String 转 LocalDate

下文会使用到的日期格式

public class FormatConstant {

	public static final String YYYY_MM_DD_HH_MM_SS_24 = "yyyy-MM-dd HH:mm:ss";

	public static final String YYYY_MM_DD_HH_MM_SS_12 = "yyyy-MM-dd hh:mm:ss";

	public static final String YYYYMMDD_HHMMSS = "yyyyMMdd HHmmss";

	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	
	public static final String YYYYMMDD = "yyyyMMdd";

	public static final String HH_MM_SS = "HH:mm:ss";

	public static final String YYYY_MM_DD_HH_MM_SS_SPECIAL = "yyyy_MM_dd&HH-mm-ss";
}



一、Date和String

使用SimpleDateFormat对象指定目标日期格式

1、Date 转 String

public void date2String() {

    // 2023-05-31 23:34:05
    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_24);
    System.out.println(formatter.format(date));

    SimpleDateFormat formatter2 = new SimpleDateFormat(YYYY_MM_DD);
    System.out.println(formatter2.format(date));

    SimpleDateFormat formatter3 = new SimpleDateFormat(HH_MM_SS);
    System.out.println(formatter3.format(date));

    SimpleDateFormat formatter4 = new SimpleDateFormat(YYYYMMDD_HHMMSS);
    System.out.println(formatter4.format(date));

    SimpleDateFormat formatter5 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_12);
    System.out.println(formatter5.format(date));

}
String、Date、LocalDate之间的转换,这一篇就够了!!!_第1张图片

2、String 转 Date

public void string2Date() throws ParseException {
    String date = "2023-05-31 22:41:21";
    SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_24);
    System.out.println(formatter.parse(date));

    String date2 = "2023-05-31";
    SimpleDateFormat formatter2 = new SimpleDateFormat(YYYY_MM_DD);
    System.out.println(formatter2.parse(date2));

    String date3 = "22:41:21";
    SimpleDateFormat formatter3 = new SimpleDateFormat(HH_MM_SS);
    System.out.println(formatter3.parse(date3));

    String date4 = "20230531 224121";
    SimpleDateFormat formatter4 = new SimpleDateFormat(YYYYMMDD_HHMMSS);
    System.out.println(formatter4.parse(date4));

    String date5 = "2023-05-31 10:41:21";
    SimpleDateFormat formatter5 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_12);
    System.out.println(formatter5.parse(date5));

    String date6 = "2023_05_31&10-41-21";
    SimpleDateFormat formatter6 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_SPECIAL);
    System.out.println(formatter6.parse(date6));
}
String、Date、LocalDate之间的转换,这一篇就够了!!!_第2张图片



二、Date和LocalDate

转变为中间对象Instant,都需要用到ZoneId时区

1、Date 转 LocalDate

public void date2LocalDate() {
    System.out.println("====LocalDate====");

    // 2023-05-31 23:36:56.896
    Date date = new Date();

    LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    System.out.println(localDate);

    LocalDate localDate1 = LocalDate.ofEpochDay(10);
    System.out.println(localDate1);

    LocalDate localDate2 = LocalDate.ofYearDay(2023,10);
    System.out.println(localDate2);


    System.out.println("====LocalDateTime====");
    LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    System.out.println(localDateTime);

    LocalDateTime localDateTime2 = LocalDateTime.of(2023, 5, 31, 23, 13, 0);
    System.out.println(localDateTime2);

    LocalDateTime localDateTime3 = LocalDateTime.ofEpochSecond(date.getTime() / 1000, 0, ZoneOffset.ofHours(8));
    System.out.println(localDateTime3);

}
String、Date、LocalDate之间的转换,这一篇就够了!!!_第3张图片

2、LocalDate 转 Date

public void localDate2Date() {
    ZoneId zone = ZoneId.systemDefault();

    System.out.println("====LocalDate====");
    LocalDate localDate = LocalDate.of(2023, 5, 31);
    Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
    System.out.println(Date.from(instant));

    LocalDate date2 = LocalDate.parse("2023-05-13", DateTimeFormatter.ISO_LOCAL_DATE);
    Instant instant2 = date2.atStartOfDay().atZone(zone).toInstant();
    System.out.println(Date.from(instant2));

    
    System.out.println("====LocalDateTime====");
    LocalDateTime localDateTime2 = LocalDateTime.of(2023, 5, 31, 23, 13, 0);
    Instant instant3 = localDateTime2.atZone(zone).toInstant();
    System.out.println(Date.from(instant3));
}
String、Date、LocalDate之间的转换,这一篇就够了!!!_第4张图片



三、LocalDate和String

使用DateTimeFormatter指定目标日期格式

1、LocalDate 转 String

public void localDate2String() {
    System.out.println("====LocalDate====");

    DateTimeFormatter df = DateTimeFormatter.ofPattern(YYYY_MM_DD);
    LocalDate localDate = LocalDate.of(2020, 2, 5);
    System.out.println(localDate.format(df));


    DateTimeFormatter df2 = DateTimeFormatter.ofPattern(YYYYMMDD);
    LocalDate localDate2 = LocalDate.of(2020, 2, 5);
    System.out.println(localDate2.format(df2));

    System.out.println("====LocalDateTime====");
    DateTimeFormatter df3 = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_12);
    LocalDateTime localDateTime = LocalDateTime.of(2020, 2, 5, 14, 30);
    System.out.println(df3.format(localDateTime));


    DateTimeFormatter df4 = DateTimeFormatter.ofPattern(YYYYMMDD_HHMMSS);
    LocalDateTime localDateTime2 = LocalDateTime.of(2020, 2, 5, 14, 30);
    System.out.println(df4.format(localDateTime2));

    DateTimeFormatter df5 = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_SPECIAL);
    LocalDateTime localDateTime3 = LocalDateTime.of(2020, 2, 5, 14, 30);
    System.out.println(df5.format(localDateTime3));
}
String、Date、LocalDate之间的转换,这一篇就够了!!!_第5张图片

2、String 转 LocalDate

public void string2localDate() {
    System.out.println("====LocalDate====");

    String dateStr = "2023-05-31";
    DateTimeFormatter df = DateTimeFormatter.ofPattern(YYYY_MM_DD);
    System.out.println(LocalDate.parse(dateStr, df));

    String dateStr2 = "20230531";
    DateTimeFormatter df2 = DateTimeFormatter.ofPattern(YYYYMMDD);
    System.out.println(LocalDate.parse(dateStr2, df2));


    System.out.println("====LocalDateTime====");
    String timeStr = "2023-05-31 16:41:21";
    DateTimeFormatter df3 = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_24);
    System.out.println(LocalDateTime.parse(timeStr, df3));


    String timeStr2 = "2023_05_31&15-21-21";
    DateTimeFormatter df4 = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_SPECIAL);
    System.out.println(LocalDateTime.parse(timeStr2, df4));
}
String、Date、LocalDate之间的转换,这一篇就够了!!!_第6张图片

你可能感兴趣的:(日常一记,java,开发语言)