标签:next pat ystemd 调整 efault orm com ext alac
年月日
时分秒
年月日时分秒
package com.lch.time;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
 * 时间转换
 */
public class TimeConvert {
    public static void main(String[] args) {
        //获取指定时间(年月日)
        LocalDate appointTime = LocalDate.of(2022, 1, 1);
        //获取现在的时间(年月日)
        LocalDate nowTime = LocalDate.now();
        //获取某日期是当周星期几(枚举类型)
        DayOfWeek dayOfWeek = nowTime.getDayOfWeek();
        //获取某日期是当年的第几天
        int dayOfYear = nowTime.getDayOfYear();
        //获取某日期是当月的几号
        int dayOfMonth = nowTime.getDayOfMonth();
        //获取年
        int year = nowTime.getYear();
        //获取月份对象(枚举) 如 :四月APRIL
        Month month = nowTime.getMonth();
        //获取月的值
        int monthValue = nowTime.getMonthValue();
        //加N天
        LocalDate localDate = nowTime.plusDays(1);
        //加N星期
        LocalDate localDate1 = nowTime.plusWeeks(1);
        //加N月
        LocalDate localDate2 = nowTime.plusMonths(1);
        //加N年
        LocalDate localDate3 = nowTime.plusYears(1);
        //减n天
        LocalDate localDate4 = nowTime.minusDays(1);
        //减n周
        LocalDate localDate5 = nowTime.minusWeeks(1);
        //减n月
        LocalDate localDate6 = nowTime.minusMonths(1);
        //减n年
        LocalDate localDate7 = nowTime.minusYears(1);
        //获取两个日期之间的间隔
        Period period = Period.between(LocalDate.of(2021, 6, 24), LocalDate.of(2021, 5, 24));
        //获取两个日期相差的天数
        int days = period.getDays();
        //获取两个日期相差的月
        int months = period.getMonths();
        //获取两个日期相差的年
        int years = period.getYears();
        /**
         * DateTimeFormatter : 日期格式化类
         */
         
        //指定格式
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //将日期转成字符串
        String nowtimeStr = dtf.format(nowTime);
        //将字符串转成日期
        TemporalAccessor newTime = dtf.parse("2020-10-10");
         /**
         * Date 转成 LocalDateTime
         */
        LocalDateTime localDateTime = new Date()
                                    .toInstant()
                                    .atZone(ZoneId.systemDefault())
                                    .toLocalDateTime();
        /**
         * LocalDateTime 转成 Date
         */
        Date date = Date.from(
                        LocalDateTime.now()
                        .atZone(ZoneId.systemDefault())
                        .toInstant()
                    );
		/**
         * Date 转成 LocalDate
         */
        LocalDate localDate = new Date()
                            .toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDate();
                    
        /**
         * LocalDate 转成 Date
         */
        Date date2 = Date.from(
                         LocalDate.now()
                         .atStartOfDay(ZoneId.systemDefault())
                         .toInstant()
                     );
        /**
         * Date 转成 LocalTime
         */
        LocalTime localTime = new Date()
                            .toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalTime();            
        /**
         * LocalTime 转成 Date
         */
        Date date1 = Date.from(
                        LocalDateTime.of(LocalDate.now(), LocalTime.now())
                        .atZone(ZoneId.systemDefault())
                        .toInstant()
                    );
	 }
}
    LocalDate localDate9 = LocalDateTime.now().toLocalDate();	
    LocalTime localTime1 = LocalDateTime.now().toLocalTime();  
        /**
         * Instant:时间戳(以Unix元年 1970年1月1日 00:00:00到某个时间毫秒值)
         */
         
        //获取当前时间,已0°经线计算
        Instant now = Instant.now();
        //转成毫秒时间
        long milli = now.toEpochMilli();
        /**
         * 获取两个时间的间隔
         */
        Duration duration = Duration.between(now, now.plusSeconds(60));
        // 两个时间间隔的秒数
        long seconds = duration.getSeconds();
        //两个时间间隔的毫秒数
        long millis = duration.toMillis();
        //两个时间间隔的天数
        long toDays = duration.toDays();
        //两个时间间隔的小时数
        long toHours = duration.toHours();
        /**
         * TemporalAdjusters(接口):时间校正器
         */
         
        //将时间调整为下周一
        
        LocalDateTime dateTime = LocalDateTime.now();
        
        LocalDateTime newDateTime = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    }
}标签:next pat ystemd 调整 efault orm com ext alac
原文地址:https://www.cnblogs.com/lch1926/p/14932468.html