jdk8_dateApi

Posted sico

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdk8_dateApi相关的知识,希望对你有一定的参考价值。

package com.zhangwl.pg0924;

import org.junit.Test;

import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

/**
* @ClassName DateApiDemo
* @Description
* @Author zhangwl
* @Date 2019/9/24 22:43
* @Version 1.0
**/
public class DateApiDemo

/*日期*/
@Test
public void test_local_date()
LocalDate nowDate = LocalDate.now();
System.out.println("今天的日期:" + nowDate);
int year = nowDate.getYear();
System.out.println("年:" + year);
int month = nowDate.getMonthValue();
System.out.println("月:" + month);
int day = nowDate.getDayOfMonth();
System.out.println("日:" + day);


/*时间*/
@Test
public void test_local_time()
LocalTime nowTime = LocalTime.now();
System.out.println("现在的时间:" + nowTime);
int hour = nowTime.getHour();
System.out.println("时:" + hour);
int minute = nowTime.getMinute();
System.out.println("分:" + minute);
int second = nowTime.getSecond();
System.out.println("秒:" + second);
int nano = nowTime.getNano();
System.out.println("毫秒:" + nano);


/*日期时间*/
@Test
public void test_local_date_time()
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println("今天日期时间:" + nowDateTime);
System.out.println("年:" + nowDateTime.getYear());
System.out.println("月:" + nowDateTime.getMonthValue());
System.out.println("日:" + nowDateTime.getDayOfMonth());
System.out.println("**********************************");
System.out.println("时:" + nowDateTime.getHour());
System.out.println("分:" + nowDateTime.getMinute());
System.out.println("秒:" + nowDateTime.getSecond());
System.out.println("毫秒:" + nowDateTime.getNano());


/*获取指定日期的日期*/
@Test
public void test_cust_local_date()
LocalDate localDate = LocalDate.of(1991, 10, 12);
System.out.println(LocalDate.ofYearDay(1991, localDate.getDayOfYear()));
System.out.println(LocalDate.ofEpochDay(localDate.toEpochDay()));//参数为距离1970-01-01的天数
System.out.println("*******************************************");
System.out.println(LocalDate.parse("1991-10-12"));
System.out.println(LocalDate.parse("19911012", DateTimeFormatter.ofPattern("yyyyMMdd")));


@Test
public void test_cust_local_time()
System.out.println(LocalTime.of(8, 20));
System.out.println(LocalTime.of(8, 20, 30));
System.out.println(LocalTime.of(8, 20, 30, 40));
System.out.println("*************************************************************");
LocalTime tTime = LocalTime.of(8, 20, 30, 40);
System.out.println(LocalTime.ofSecondOfDay(tTime.toSecondOfDay()));//参数为距离当天零时的秒数
System.out.println(LocalTime.ofNanoOfDay(tTime.toNanoOfDay()));
System.out.println("*************************************************************");
System.out.println(LocalTime.parse("08:20:30"));
System.out.println(LocalTime.parse("082030", DateTimeFormatter.ofPattern("HHmmss")));


@Test
public void test_cust_local_date_time()
System.out.println(LocalDateTime.of(1991, 10, 12, 8, 20, 30));
System.out.println(LocalDateTime.of(1991, Month.OCTOBER, 8, 20, 30, 150));
System.out.println(LocalDateTime.parse("1991-10-12 08:20:30", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));


/*日期的比较*/
@Test
public void test_comp_local_date()
LocalDate myDate = LocalDate.of(1991, 10, 12);
LocalDate nowDate = LocalDate.now();
/*比较两个日期*/
System.out.println("今天是1991-10-12吗?" + nowDate.equals(myDate));

以上是关于jdk8_dateApi的主要内容,如果未能解决你的问题,请参考以下文章