其他分享
首页 > 其他分享> > 开发记录:涉及到日期时间以及星期转换

开发记录:涉及到日期时间以及星期转换

作者:互联网

开发记录:Date与DateTime以及星期转换

Date和DateTime

@Test
public void test(){
    System.out.println(new Date());
    System.out.println(new DateTime());
}
Sun Feb 20 21:00:53 CST 2022
2022-02-20T21:00:53.310+08:00

Date日期与DateTime转换

/**
* 将Date日期(yyyy-MM-dd HH:mm)转换为DateTime
*/
private DateTime getDateTime(Date date, String timeString) {
    String dateTimeString = new DateTime(date).toString("yyyy-MM-dd") + " "+ timeString;
    DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm").parseDateTime(dateTimeString);
    return dateTime;
}

根据日期获取周几数据

/**
* 根据日期获取周几数据
* @param dateTime
* @return
*/
private String getDayOfWeek(DateTime dateTime) {
    String dayOfWeek = "";
    switch (dateTime.getDayOfWeek()) {
        case DateTimeConstants.SUNDAY:
            dayOfWeek = "周日";
            break;
        case DateTimeConstants.MONDAY:
            dayOfWeek = "周一";
            break;
        case DateTimeConstants.TUESDAY:
            dayOfWeek = "周二";
            break;
        case DateTimeConstants.WEDNESDAY:
            dayOfWeek = "周三";
            break;
        case DateTimeConstants.THURSDAY:
            dayOfWeek = "周四";
            break;
        case DateTimeConstants.FRIDAY:
            dayOfWeek = "周五";
            break;
        case DateTimeConstants.SATURDAY:
            dayOfWeek = "周六";
        default:
            break;
    }
    return dayOfWeek;
}

例子:注意datetime与date的区别

this.getDayOfWeek(new DateTime(this.getDateTime(date, "18:25")));

标签:星期,dayOfWeek,转换,DateTimeConstants,DateTime,break,case,日期,Date
来源: https://www.cnblogs.com/wenhaozou/p/15932172.html