Java String、Date、Calendar、LocalDate和LocalDateTime互转

这篇具有很好参考价值的文章主要介绍了Java String、Date、Calendar、LocalDate和LocalDateTime互转。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

一、String和Date互转

1.1 String时间转为Date

1.2 Date转为String

二、String和Calendar互转

2.1 String时间转为Calendar

2.2 Calendar转为String

三、String和LocalDate(LocalDateTime)互转

3.1 String时间转为LocalDate(LocalDateTime)

3.2 LocalDate(LocalDateTime)转为String

四、Date和Calendar互转

4.1 Date转为Calendar

4.2 Calendar转为Date

五、Date和LocalDate(LocalDateTime)互转

5.1 Date转为LocalDate(LocalDateTime)

5.2 LocalDate(LocalDateTime)转为Date

六、Calendar和LocalDat(LocalDateTime)互转

6.1 Calendar转为LocalDate(LocalDateTime)

6.2 LocalDat(LocalDateTime)转为Calendar

七、LocalDate和LocalDateTime互转

7.1 LocalDate转LocalDateTime

7.2 LocalDateTime转LocalDate


一、String和Date互转

1.1 String时间转为Date
private Date stringToDate(String dateStr, String pattern) {
    try {
        return new SimpleDateFormat(pattern).parse(dateStr);
    } catch (Throwable ignore) {
        // TODO 时间转换异常日期
    }
    return null;
}

调方法 stringToDate(Sring, pattern):

System.out.println(stringToDate("2023-11", "yyyy-MM"));
System.out.println(stringToDate("2023-11-11", "yyyy-MM-dd"));
System.out.println(stringToDate("2023-11-11 11", "yyyy-MM-dd hh"));
System.out.println(stringToDate("2023-11-11 11:11", "yyyy-MM-dd hh:mm"));
System.out.println(stringToDate("2023-11-11 11:11:11", "yyyy-MM-dd hh:mm:ss"));

注:第一个参数的格式必须与第二个参数的模式一致才能正常转换

运行结果:

Wed Nov 01 00:00:00 CST 2023
Sat Nov 11 00:00:00 CST 2023
Sat Nov 11 11:00:00 CST 2023
Sat Nov 11 11:11:00 CST 2023
Sat Nov 11 11:11:11 CST 2023
1.2 Date转为String
private String dateToString(Date date, String pattern) {
    return new SimpleDateFormat(pattern).format(date);
}

调方法dateToString(Date, pattern),可根据第二个参数模式来获取对应模式的时间对应字符串格式:

System.out.println(dateToString(new Date(), "yyyy"));
System.out.println(dateToString(new Date(), "yyyy-MM"));
System.out.println(dateToString(new Date(), "yyyy-MM-dd"));
System.out.println(dateToString(new Date(), "yyyy-MM-dd hh"));
System.out.println(dateToString(new Date(), "yyyy-MM-dd hh:mm"));
System.out.println(dateToString(new Date(), "yyyy-MM-dd hh:mm:ss"));

运行结果:

2023
2023-08
2023-08-01
2023-08-01 11
2023-08-01 11:40
2023-08-01 11:40:26

二、String和Calendar互转

2.1 String时间转为Calendar
private Calendar stringToCalendar(String dateStr, String pattern) {
    try {
        SimpleDateFormat sdf= new SimpleDateFormat(pattern);
        Date date = sdf.parse(dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return calendar;
    } catch (Throwable ignore) {
        // TODO 时间字符串转Calendar异常
    }
    return null;
}

调用方法stringToCalendar(String, pattern):

Calendar calendar0 = stringToCalendar("2023-08-05", "yyyy");
Calendar calendar1 = stringToCalendar("2023-08-05", "yyyy-MM");
Calendar calendar2 = stringToCalendar("2023-08-05 11:11:11", "yyyy-MM-dd hh:mm:ss");

System.out.println(calendar0.getTime());
System.out.println(calendar1.getTime());
System.out.println(calendar2.getTime());

注:第一个参数的格式与第二个参数的模式不一定要求强一致,但若String只有年月日,模式不能有时分秒

运行结果:

Sun Jan 01 00:00:00 CST 2023
Tue Aug 01 00:00:00 CST 2023
Sat Aug 05 11:11:11 CST 2023

说明:根据日期模式格式化时间对象时,若模式只有年月则日为1号,若模式没有年份则月日为01月01日,模式没有时/分/秒 则为0

2.2 Calendar转为String
private String calendarToString(Calendar calendar, final String pattern) {

    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    return sdf.format(calendar.getTime());
}

调用方法calendarToString(Calendar, pattern),第二个参数为输出的格式模式:

System.out.println(calendarToString(Calendar.getInstance(), "yyyy-MM"));
System.out.println(calendarToString(Calendar.getInstance(), "yyyy-MM-dd"));
System.out.println(calendarToString(Calendar.getInstance(), "yyyy-MM-dd hh"));
System.out.println(calendarToString(Calendar.getInstance(), "yyyy-MM-dd hh:mm"));
System.out.println(calendarToString(Calendar.getInstance(), "yyyy-MM-dd hh:mm:ss"));

运行结果:

2023-08
2023-08-01
2023-08-01 11
2023-08-01 11:59
2023-08-01 11:59:13

三、String和LocalDate(LocalDateTime)互转

3.1 String时间转为LocalDate(LocalDateTime)
private LocalDate stringToLocalDate(String dateStr, String pattern) {

    DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
    return LocalDate.parse(dateStr, df);
}

private LocalDateTime stringToLocalDateTime(String dateStr, String pattern) {

    DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
    return LocalDateTime.parse(dateStr, df);
}

调方法 stringToLocalDate(Sring, pattern),第二个参数模式必须有年月日,第一个参数的格式必须与第二个参数的模式一致才能正常转换

System.out.println(stringToLocalDate("2023-08-01", "yyyy-MM-dd"));
System.out.println(stringToLocalDate("2023-08-02 11", "yyyy-MM-dd hh"));
System.out.println(stringToLocalDate("2023-08-02 11:12", "yyyy-MM-dd hh:mm"));
System.out.println(stringToLocalDate("2023-08-02 11:12:13", "yyyy-MM-dd hh:mm:ss"));

System.out.println(stringToLocalDateTime("2023-08-02 11:12:13", "yyyy-MM-dd hh:mm:ss"));

运行结果:

2023-08-01
2023-08-02
2023-08-02
2023-08-02

java.time.format.DateTimeParseException: Text '2023-08-02 11:12:13' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=12, SecondOfMinute=13, MilliOfSecond=0, MicroOfSecond=0, HourOfAmPm=11, NanoOfSecond=0},ISO resolved to 2023-08-02 of type java.time.format.Parsed

注:报错的原因是stringToLocalDateTime方法第二个参数模式的小时hh 采用12小时,反解析时不知道上午还是下午,改成24小时制“yyyy-MM-dd HH:mm:ss”即可。

3.2 LocalDate(LocalDateTime)转为String
private String localDateToString(LocalDate localDate, String pattern) {

    return localDate.format(DateTimeFormatter.ofPattern(pattern));
}

private String localDateToString(LocalDateTime localDateTime, String pattern) {

    return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}

只需要年月日用LocalDate,若需要时分秒用LocalDateTime

调方法localDateToString:

System.out.println(localDateToString(LocalDate.now(), "yyyy"));
System.out.println(localDateToString(LocalDate.now(), "yyyy-MM"));
System.out.println(localDateToString(LocalDate.now(), "yyyy-MM-dd"));
System.out.println(localDateToString(LocalDateTime.now(), "yyyy-MM-dd HH"));
System.out.println(localDateToString(LocalDateTime.now(), "yyyy-MM-dd HH:mm"));
System.out.println(localDateToString(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));

运行结果:

2023
2023-08
2023-08-01
2023-08-01 12
2023-08-01 12:03
2023-08-01 12:03:44

四、Date和Calendar互转

4.1 Date转为Calendar
private Calendar dateToCalendar(Date date) {

    Calendar cal = Calendar.getInstance();
    cal.setTime(date );

    return cal;
}

调方法dateToCalendar(Date):

Calendar calendar = dateToCalendar(new Date());
System.out.println(calendar.getTime());

运行结果:

Tue Aug 01 12:08:41 CST 2023
4.2 Calendar转为Date
private Date calendarToDate(Calendar calendar) {

    return calendar.getTime();
}

调方法calendarToDate(Calendar):

System.out.println(calendarToDate(Calendar.getInstance()));

运行结果:

Tue Aug 01 12:10:21 CST 2023

五、Date和LocalDate(LocalDateTime)互转

5.1 Date转为LocalDate(LocalDateTime)
private LocalDate dateToLocalDate(Date date) {

    // 将java.util.Date转换为ZonedDateTime。
    Instant instant = date.toInstant();
    ZoneId zoneId = ZoneId.systemDefault();
    ZonedDateTime zonedDateTime = instant.atZone(zoneId);
    // 使用它的toLocalDate()方法从ZonedDateTime获取LocalDate

    return zonedDateTime.toLocalDate();
}

private LocalDateTime dateToLocalDateTime(Date date) {

    // 将Date对象转换成为Instant对象
    Instant instant = date.toInstant();
    // 将瞬时对象转换成为LocalDateTime对象
    return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}

调dateToLocalDate(Date),转LocalDateTime调dateToLocalDateTime(Date):

System.out.println(dateToLocalDate(new Date()));
System.out.println(dateToLocalDateTime(new Date()));

运行结果:

2023-08-01
2023-08-01T12:22:39.742
5.2 LocalDate(LocalDateTime)转为Date
private Date localDateToDate(LocalDate localDate) {
    ZoneId zoneId = ZoneId.systemDefault();
    ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId);
    // 将时区对象转换成为瞬时对象
    Instant instant2 = zonedDateTime.toInstant();
    // 通过Date提供的静态方法将瞬时对象转换为Date对象
    return Date.from(instant2);
}

private Date localDateTimeToDate(LocalDateTime localDateTime) {

    // 将LocalDateTime对象转换成为时区对象
    ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
    // 将时区对象转换成为瞬时对象
    Instant instant2 = zonedDateTime.toInstant();
    // 通过Date提供的静态方法将瞬时对象转换为Date对象
    return Date.from(instant2);
}

LocalDateTime调localDateTimeToDate(LocalDateTime ),LocalDate调localDateToDate(LocalDate ):

System.out.println(localDateToDate(LocalDate.now()));
System.out.println(localDateTimeToDate(LocalDateTime.now()));

运行结果:

Tue Aug 01 00:00:00 CST 2023
Tue Aug 01 12:25:06 CST 2023

六、Calendar和LocalDat(LocalDateTime)互转

6.1 Calendar转为LocalDate(LocalDateTime)

调4.2由Calendar转为Date,再调5.1转为LocalDate/LocalDateTime:

System.out.println(dateToLocalDate(calendarToDate(Calendar.getInstance())));
System.out.println(dateToLocalDateTime(calendarToDate(Calendar.getInstance())));

运行结果:

2023-08-01
2023-08-01T12:33:01.151
6.2 LocalDat(LocalDateTime)转为Calendar

调5.2由LocalDate转为Date,再调4.1转为Calendar:

Calendar calendar1 = dateToCalendar(localDateToDate(LocalDate.now()));
Calendar calendar2 = dateToCalendar(localDateTimeToDate(LocalDateTime.now()));

System.out.println(calendar1.getTime());
System.out.println(calendar2.getTime());

运行结果:

Tue Aug 01 00:00:00 CST 2023
Tue Aug 01 12:35:47 CST 2023

七、LocalDate和LocalDateTime互转

7.1 LocalDate转LocalDateTime
private LocalDateTime localDateToLocalDateTime(LocalDate localDate) {
    return dateToLocalDateTime(localDateToDate(localDate));
}
7.2 LocalDateTime转LocalDate
private LocalDate localDateTimeToLocalDate(LocalDateTime localDateTime) {
    return localDateTime.toLocalDate();
}

调7.1和7.2:

System.out.println(localDateToLocalDateTime(LocalDate.now()));
System.out.println(localDateTimeToLocalDate(LocalDateTime.now()));

运行结果:

2023-08-01T00:00
2023-08-01

注:String为java.lang.String,Date为java.util.Date,Calendar为java.util.Calendar,LocalDate为java.time.LocalDate,LocalDateTime为java.time.LocalDateTime。文章来源地址https://www.toymoban.com/news/detail-741501.html

到了这里,关于Java String、Date、Calendar、LocalDate和LocalDateTime互转的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包