LocalDateTime获取 年月日时分秒和判断日期大小

环境:java version "13.0.1"。

创建一个DateUtils类,提供三个常用方法:

  • String 转换 LocalDateTime的方法。

  • 获取LocalDateTime获取 年月日时分秒。

  • 两个时间的间隔毫秒数,常用于判断日期大小。

import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    private static DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");

    public static void getTimeByDate() {
        Date date = new Date();
        DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日
        System.out.println(df1.format(date));
        DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
        System.out.println(df2.format(date));
        DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒
        System.out.println(df3.format(date));
        DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒)
        System.out.println(df4.format(date));
        DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); //显示日期,上下午,时间(精确到秒)
        System.out.println(df5.format(date));
        DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); //显示日期,上下午,时间(精确到分)
        System.out.println(df6.format(date));
        DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); //显示日期,时间(精确到分)
        System.out.println(df7.format(date));
    }

    public static void getTimeByCalendar() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);//获取年份
        int month = cal.get(Calendar.MONTH);//获取月份
        int day = cal.get(Calendar.DATE);//获取日
        int hour = cal.get(Calendar.HOUR);//小时
        int minute = cal.get(Calendar.MINUTE);//分
        int second = cal.get(Calendar.SECOND);//秒
        int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
        System.out.println("现在的时间是:公元" + year + "年" + month + "月" + day + "日," + hour + "时" + minute + "分" + second + "秒,    一周的第几天:" + WeekOfYear);
    }

    /**
     * 获取指定时间的年月日等
     * @param localDateTime 指定时间
     */
    public static void getTimeByGivenLocalDateTime(LocalDateTime localDateTime) {
        if (null == localDateTime) {
            localDateTime = LocalDateTime.now();
        }
        int dayOfYear = localDateTime.getDayOfYear();
        int dayOfMonth = localDateTime.getDayOfMonth();
        DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
        System.out.println("今天是" + localDateTime + "\n"
                + "本年当中第" + dayOfYear + "天" + "\n"
                + "本月当中第" + dayOfMonth + "天" + "\n"
                + "本周中星期" + dayOfWeek.getValue() + "-即" + dayOfWeek + "\n");

        //获取指定时间的年月日时分秒
        int year = localDateTime.getYear();
        Month month = localDateTime.getMonth();
        int day = localDateTime.getDayOfMonth();
        int hour = localDateTime.getHour();
        int minute = localDateTime.getMinute();
        int second = localDateTime.getSecond();
        System.out.println("今天是" + localDateTime + "\n"
                + "年 : " + year + "\n"
                + "月 : " + month.getValue() + "-即 " + month + "\n"
                + "日 : " + day + "\n"
                + "时 : " + hour + "\n"
                + "分 : " + minute + "\n"
                + "秒 : " + second + "\n"
        );
    }
    /**
     * String 转换 LocalDateTime
     * @param dateStr
     * @return
     */
    public static LocalDateTime string2Time(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        LocalDateTime time = LocalDateTime.parse(dateStr, fmt);
        System.out.println("time:" + time);
        return time;
    }

    /**
     * 求时间间隔,返回值大于零说明结束时间晚于开始时间
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @return 毫秒,等于endTime 减去 beginTime后的毫秒数
     */
    public static Long between(LocalDateTime beginTime, LocalDateTime endTime) {
        Duration duration = Duration.between(beginTime, endTime);
        return duration.toMillis();
    }

    public static void main(String[] args) {
        getTimeByDate();
        System.out.println("********getTimeByCalendar********************");
        getTimeByCalendar();
        System.out.println("********getTimeByGivenLocalDateTime****************");
        String str = "2021-10-09 15:32:06:777";
        LocalDateTime ldt = string2Time(str);
        getTimeByGivenLocalDateTime(ldt);
        System.out.println("********between****************");
        str = "2021-10-09 17:32:06:777";
        LocalDateTime endTime = string2Time(str);
        System.out.println(between(ldt, endTime));
    }

}

测试结果:

2021年10月10日
2021年10月10日 下午12:06:02
下午12:06:02
2021年10月10日星期日 中国标准时间 下午12:06:02
2021年10月10日 CST 下午12:06:02
2021/10/10 下午12:06
2021年10月10日 下午12:06:02
********getTimeByCalendar********************
现在的时间是:公元2021年9月10日,0时6分2秒,    一周的第几天:1
********getTimeByGivenLocalDateTime****************
time:2021-10-09T15:32:06.777
今天是2021-10-09T15:32:06.777
本年当中第282天
本月当中第9天
本周中星期6-即SATURDAY

今天是2021-10-09T15:32:06.777
年 : 2021
月 : 10-即 OCTOBER
日 : 9
时 : 15
分 : 32
秒 : 6

********between****************
time:2021-10-09T17:32:06.777
7200000

Process finished with exit code 0
posted @ 2021-10-10 12:09  楼兰胡杨  阅读(5980)  评论(0编辑  收藏  举报