码迷,mamicode.com
首页 > Windows程序 > 详细

jdk8_dateApi

时间:2019-09-26 00:03:58      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:nan   rip   指定   epo   eth   local   pattern   equals   test   

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

标签:nan   rip   指定   epo   eth   local   pattern   equals   test   

原文地址:https://www.cnblogs.com/sico/p/11588345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!