如何借助LocalDateTime获取"年月日时分秒"和"判断日期大小"的示例

乔欣 Java经验 发布时间:2023-02-21 20:44:38 阅读数:2749 1
下文笔者讲述使用LocalDateTime获取年月日时分秒及判断日期大小的示例分享,如下所示
实现思路:
    使用Duration.between方法即可对比两个日期
 
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 = "2023-10-13 16:44:22:666";
        LocalDateTime ldt = string2Time(str);
        getTimeByGivenLocalDateTime(ldt);
        System.out.println("********between****************");
        str = "2023-9-09 15:44:14:123";
        LocalDateTime endTime = string2Time(str);
        System.out.println(between(ldt, endTime));
    }


}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202302/16769835225871.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者