2023-03-31 Java jdk8以前和8 时间类练习

2023-03-30  本文已影响0人  AndYMJ

public class DateDemo {

    public static void main(String[] args) {

//        JDK8之前Date

// public Date()                    创建一个Date对象,代表的是系统当前此刻日期时间。

        Date date =new Date();

System.out.println(date);

// public Date(long time)          把时间毫秒值转换成Date日期对象。

        long l =System.currentTimeMillis();

Date date1 =new Date(l);

System.out.println(date1);

// public long getTime()            获取Date对象中的毫秒值

        long time =date1.getTime();

System.out.println(time);

// public void setTime(long time)    修改日期对象的时间为当前时间毫秒值对应的时间

        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String time1 ="2023-11-10 23:44:23";

try {

            Date parse =sdf.parse(time1);

System.out.println(parse);

} catch (ParseException e) {

            e.printStackTrace();

}

//        SimpleDateFormat

// public SimpleDateFormat(String  pattern)        创建简单日期格式化对象,并封装时间的格式

        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

// public final String format(Date date)            将日期格式化成日期/时间字符串

        String format =sdf1.format(new Date());

System.out.println(format);

// public final String format(Object time)          将时间毫秒值式化成日期/时间字符串

        long time2 =date.getTime();

String format1 =sdf1.format(time2);

System.out.println(format1);

// public Date parse(String source)                把字符串时间解析成日期对象

        Date format2 =null;

try {

            format2 =sdf1.parse("2012年6月20日 13时24分30秒");

} catch (ParseException e) {

            e.printStackTrace();

}

        System.out.println(format2);

//                Calendar

// public  static Calendar getInstance()    获取当前日历对象

        Calendar calendar =Calendar.getInstance();

// public int get(int field)    获取日历中的某个信息。

        int YEAR =calendar.get(Calendar.YEAR);

int MONTH =calendar.get(Calendar.MONTH);

int DAY_OF_MONTH =calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(YEAR+"--"+MONTH+"---"+DAY_OF_MONTH);

// public final Date getTime()      获取日期对象。

        Date time3 =calendar.getTime();

// public long getTimeInMillis()    获取时间毫秒值

        long timeInMillis =calendar.getTimeInMillis();

// public void set(int field,int value)      修改日历的某个信息。

        calendar.set(Calendar.YEAR,2024);

System.out.println(calendar.get(Calendar.YEAR));

// public void add(int field,int amount)    为某个信息增加/减少指定的值

        calendar.add(Calendar.YEAR,2);

System.out.println(calendar.get(Calendar.YEAR));

//        JDK8

//        LocalDate,LocalTime,LocalDateTime 对象的获取

// public static Xxxx now(): 获取系统当前时间对应的该对象

        LocalDate localDate =LocalDate.now();

LocalTime localTime =LocalTime.now();

LocalDateTime localDateTime =LocalDateTime.now();

// public static Xxxx of(…):获取指定时间的对象

        LocalDate localDate1 =LocalDate.of(2021,10,10);

LocalTime localTime1 =LocalTime.of(14,54,21);

LocalDateTime localDateTime1 =LocalDateTime.of(2021,10,10,14,54,21);

//                LocalDate

// public int geYear()                                      获取年

        LocalDate localDate2 =LocalDate.now();

int year =localDate2.getYear();

System.out.println(year);

// public int getMonthValue()                              获取月份(1-12)

        int monthValue =localDate.getMonthValue();

System.out.println(monthValue);

// public int getDayOfMonth()                              获取日

        int dayOfYear =localDate.getDayOfMonth();

System.out.println(dayOfYear);

// public int getDayOfYear()                                获取当前是一年中的第几天

        System.out.println(localDate.getDayOfYear());

// public DayOfWeek getDayOfWeek()    获取星期几:ld.getDayOfWeek().getValue()

        int value =localDate.getDayOfWeek().getValue();

System.out.println(value);

// withYear、withMonth、withDayOfMonth、withDayOfYear        直接修改某个信息,返回新日期对象

        LocalDate localDate3 =localDate.withYear(1998).withMonth(11).withDayOfMonth(28).withDayOfYear(220);

System.out.println(localDate3);

int year1 =localDate3.getYear();

System.out.println(year1);

// plusYears、plusMonths、plusDays、plusWeeks                把某个信息加多少,返回新日期对象

        LocalDate localDate4 =localDate.plusYears(2).plusMonths(4).plusDays(10).plusWeeks(2);

System.out.println(localDate4);

// minusYears、minusMonths、minusDays,minusWeeks            把某个信息减多少,返回新日期对象

        LocalDate localDate5 =

localDate1.minusYears(1).minusMonths(1).minusDays(1).minusWeeks(2);

System.out.println(localDate5);

// equals isBefore isAfter

        boolean equals =localDate1.equals(localDate2);

System.out.println(equals);

//        LocalTime

        LocalTime lTime2 =LocalTime.now();

// public int getHour()                              获取小时

        int hour =lTime2.getHour();

System.out.println(hour);

// public int getMinute()                            获取分

        int minute =lTime2.getMinute();

System.out.println(minute);

// public int getSecond()                            获取秒

        int second =lTime2.getSecond();

System.out.println(second);

// public int getNano()                              获取纳秒

        int nano =lTime2.getNano();

System.out.println(nano);

// withHour、withMinute、withSecond、withNano          修改时间,返回新时间对象

        LocalTime localTime2 =lTime2.withHour(2).withMinute(10).withSecond(5).withNano(20);

System.out.println(localTime2);

// plusHours、plusMinutes、plusSeconds、plusNanos      把某个信息加多少,返回新时间对象

        LocalTime localTime3 =localTime2.plusHours(4).plusMinutes(20).plusSeconds(13).withNano(11);

System.out.println(localTime3);

// minusHours、minusMinutes、minusSeconds、minusNanos  把某个信息减多少,返回新时间对象

        LocalTime localTime4 =localTime3.minusHours(4).minusMinutes(2).minusSeconds(10).minusNanos(10);

System.out.println(localTime4);

// equals isBefore isAfter                            判断2个时间对象,是否相等,在前还是在后

        boolean equals1 =lTime2.equals(localTime);

System.out.println(equals1);

//                LocalDateTime

// getYear、getMonthValue、getDayOfMonth、getDayOfYear  getDayOfWeek、getHour、getMinute、getSecond、getNano获取年月日、时分秒、纳秒等

// withYear、withMonth、withDayOfMonth、withDayOfYear  withHour、withMinute、withSecond、withNano修改某个信息,返回新日期时间对象

// plusYears、plusMonths、plusDays、plusWeeks  plusHours、plusMinutes、plusSeconds、plusNanos把某个信息加多少,返回新日期时间对象

// minusYears、minusMonths、minusDays、minusWeeks  minusHours、minusMinutes、minusSeconds、minusNanos把某个信息减多少,返回新日期时间对象

// equals isBefore isAfter判断2个时间对象,是否相等,在前还是在后

        /**

        * LocalDateTime转换

          */

// public LocalDate toLocalDate()转换成一个LocalDate对象

// public LocalTime toLocalTime()转换成一个LocalTime对象

// public static LocalDateTime of(LocalDate date, LocalTime time)将LocalDate对象和LocalTime对象转换为LocalDateTime

              /*

* DateTimeFormatter

* */

// public static DateTimeFormatter ofPattern(时间格式yyyy....)      获取格式化器对象

// public String format(时间对象)                                  格式化时间

        /*

        *LocalDateTime,LocalTime,LocalDate提供的使用DateTimeFormatter的方法* */

// public String format(DateTimeFormatter formatter)                        格式化时间

// public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)解析

        /*

Period

*/

// public static Period between(LocalDate start, LocalDate end) 传入2个日期对象,得到Period对象

// public  int getYears()      计算隔几年,并返回

// public  int getMonths()      计算隔几个月,年返回

// public  int getDays()        计算隔多少天,并返回

                /*

Duration

*/

// public  static Duration between(开始时间对象1,截止时间对象2)    传入2个时间对象,得到Duration对象

// public  long toDays()                                        计算隔多少天,并返回

// public  long toHours()                                      计算隔多少小时,并返回

// public  long toMinutes()                                    计算隔多少分,并返回

// public  long toSeconds()                                    计算隔多少秒,并返回

// public  long toMillis()                                      计算隔多少毫秒,并返回

// public  long toNanos()                                      计算隔多少纳秒,并返回

        /*

* ZoneId

* */

// public static Set<String>  getAvailableZoneIds() 获取Java中支持的所有时区

// public static ZoneId systemDefault()            获取系统默认时区

// public static ZoneId of(String zoneId)          获取一个指定时区

                /*

* ZoneDateTime

* */

// public static ZonedDateTime now()            获取当前系统时区的ZonedDateTime对象

// public static ZonedDateTime now(ZoneId zone) 获取指定时区的ZonedDateTime对象

// getYear、getMonthValue、getDayOfMonth、getDayOfYeargetDayOfWeek、getHour、getMinute、getSecond、getNano获取年月日、时分秒、纳秒等

// public ZonedDateTime withXxx(时间)                修改时间系列的方法

// public ZonedDateTime minusXxx(时间)                减少时间系列的方法

// public ZonedDateTime plusXxx(时间)                增加时间系列的方法

    }

}

上一篇 下一篇

猜你喜欢

热点阅读