JAVA常用类 日期类(SimpleDateFormat、Calendar、LocalDate、LocalTime、LocalDateTime 、Instant、DateTimeFormatter类)

JAVA常用类 日期类(SimpleDateFormat、Calendar、LocalDate、LocalTime、LocalDateTime 、Instant、DateTimeFormatter类),第1张

JAVA常用类 日期类(SimpleDateFormat、Calendar、LocalDate、LocalTime、LocalDateTime 、Instant、DateTimeFormatter类) SimpleDateFormat类

Calendar类

SimpleDateFormat类 和 Calendar类 的代码分析
package java1;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateTimeTest {
    
    @Test
    public void testSimpleDateFormat() throws ParseException {
        //实例化SimpleDateFormat:使用默认的构造器
        SimpleDateFormat sdf = new SimpleDateFormat();

        //格式:日期 --->字符串
        Date date = new Date();
        System.out.println(date);//默认打印当前时间  Sat Jan 29 18:14:08 CST 2022

        String format = sdf.format(date);//将日期格式化
        System.out.println(format);//打印出格式化后的字符串  22-1-29 下午6:14

        //解析:格式化的逆过程,字符串 ---> 日期
        String str = "22-1-29 下午6:14";
        Date date1 = sdf.parse(str);//将字符串解析成日期
        System.out.println(date1);//Sat Jan 29 18:14:00 CST 2022

        /
    @Test
    public void testCalendar(){
        //1.实例化//Calendar是一个抽象类
        //方式一:创建其子类(GregorianCalendar)的对象
        //方式二:调用其静态方法getInstance()
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getClass());//class java.util.GregorianCalendar

        //2.常用方法
        //get()
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        //DAY_OF_MONTH是static类型的的属性,含义→这个月的第几天,今天2022-1-29。
        System.out.println(days);//输出 29
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//这一年的第几天,输出29

        //set() 将指定的属性设计为新的值
        //calendar可变性 直接在原来的数据上改了
        calendar.set(Calendar.DAY_OF_MONTH,22);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);// 22

        //add() 在现有的上加或者减(加负数)多少天
        calendar.add(Calendar.DAY_OF_MONTH,-3);//22+(-3)=19
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //getTime():日历类---> Date
        Date date = calendar.getTime();
        System.out.println(date);//Wed Jan 19 21:03:28 CST 2022

        //setTime():Date ---> 日历类
        Date date1 = new Date();
        calendar.setTime(date1);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);//这个月的第几天, 29 天

    }
}

jdk8中新日期时间api LocalDate年月日、LocalTime时分秒、LocalDateTime年月日时分秒




Instant类的使用


DateTimeFormatter类 日期时间格式化类

上述日期类的代码 使用举例
package java1;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Date;


public class JDK8DateTimeTest {

    @Test
    public void testDate(){
        //偏移量 写0是一月,9是十月 ,年份偏移量加1900了。
        Date date1 = new Date(2020 - 1900,9 - 1,8);
        System.out.println(date1);//Tue Sep 08 00:00:00 GMT+08:00 2020
    }

    
    @Test
    public void test1(){
        //now():获取当前的日期、时间、日期+时间 的实例化对象
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);//2022-01-29
        System.out.println(localTime);//21:25:11.342
        System.out.println(localDateTime);//2022-01-29T21:25:11.343

        //of():设置指定的年、月、日、时、分、秒。没有偏移量 的实例化对象
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
        System.out.println(localDateTime1);//2020-10-06T13:23:43


        //getXxx():获取相关的属性
        System.out.println(localDateTime.getDayOfMonth());//29 //获取当月的第几天
        System.out.println(localDateTime.getDayOfWeek());//SATURDAY //这周的第几天
        System.out.println(localDateTime.getMonth());//JANUARY //第几月
        System.out.println(localDateTime.getMonthValue());//1 //第几月数字的
        System.out.println(localDateTime.getMinute());//25 //获取分钟

        //体现不可变性,在修改数据后返回一个新的对象
        //withXxx():设置相关的属性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate);//2022-01-29
        System.out.println(localDate1);//2022-01-22


        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime);//2022-01-29T21:25:11.343
        System.out.println(localDateTime2);//2022-01-29T04:25:11.343

        //不可变性
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);//加(plus)三个月
        System.out.println(localDateTime);//2022-01-29T21:25:11.343
        System.out.println(localDateTime3);//2022-04-29T21:25:11.343

        LocalDateTime localDateTime4 = localDateTime.minusDays(6);//减(minus)6个月
        System.out.println(localDateTime);//2022-01-29T21:25:11.343
        System.out.println(localDateTime4);//2022-01-23T21:25:11.343
    }

    
    @Test
    public void test2(){
        //now():获取本初子午线对应的标准时间 ,实例化一个对象
        Instant instant = Instant.now();
        System.out.println(instant);//2022-01-29T13:40:23.249Z (和北京时间差8个小时,算的是本初子午线的时间)

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2022-01-29T21:40:23.249+08:00

        //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数  ---> Date类的getTime()
        long milli = instant.toEpochMilli();
        System.out.println(milli);//1643463623249

        //ofEpochMilli():通过给定的毫秒数,获取Instant实例  -->Date(long millis)
        Instant instant1 = Instant.ofEpochMilli(1643463623249L);
        System.out.println(instant1);//2022-01-29T13:40:23.249Z
    }


    

    @Test
    public void test3(){
//        方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str1 = formatter.format(localDateTime);//以ISO_LOCAL_DATE_TIME的格式 格式化
        System.out.println(localDateTime);//2022-01-29T21:51:15.212
        System.out.println(str1);//2022-01-29T21:51:15.212

        //解析:字符串 -->日期
        TemporalAccessor parse = formatter.parse("2022-01-29T21:51:15.212");//以ISO_LOCAL_DATE_TIME的格式 解析
        System.out.println(parse);//2022-01-29T21:51:15.212

//        方式二:
//        本地化相关的格式。如:ofLocalizedDateTime()
//        FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        //格式化
        String str2 = formatter1.format(localDateTime);//以FormatStyle.LONG的格式 格式化
        System.out.println(str2);//2022年1月29日 下午09时53分35秒


//      本地化相关的格式。如:ofLocalizedDate()
//      FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        //格式化
        String str3 = formatter2.format(LocalDate.now());//以FormatStyle.MEDIUM格式 格式化
        System.out.println(str3);//2022-1-29


//       重点: 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String str4 = formatter3.format(LocalDateTime.now());//以"yyyy-MM-dd hh:mm:ss"格式 格式化
        System.out.println(str4);//2022-01-29 09:53:35

        //解析
        TemporalAccessor accessor = formatter3.parse("2022-01-29 09:53:35");//以"yyyy-MM-dd hh:mm:ss"格式 解析
        System.out.println(accessor);
        //{NanoOfSecond=0, MilliOfSecond=0, MinuteOfHour=53, SecondOfMinute=35, MicroOfSecond=0, HourOfAmPm=9},ISO resolved to 2022-01-29
        //也就是 2022-01-29
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5717289.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存