Loading

1 2

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

开发记录: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")));
posted @ 2022-02-24 15:56  Komorebi_WH  阅读(49)  评论(0编辑  收藏  举报