Java8时间的简单时间

package com.java8.date;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.*;

public class DateTest {

    @Test
    public void LocalDateTest() {

        // of方法获取一个指定日期的LocalDate
        LocalDate date1 = LocalDate.of(2018, 12, 29);
        System.out.println(date1);
        System.out.println(date1.getYear());
        System.out.println(date1.getMonth());

        System.out.println(date1.getMonthValue());
        System.out.println(date1.getDayOfMonth());
        System.out.println(date1.getDayOfWeek());
        System.out.println(date1.getDayOfWeek().getValue());
        System.out.println(date1.getDayOfYear());

        System.out.println("判断时间前后关系:" + date1.isBefore(date1));

        // now 获取当前时间
        System.out.println(LocalDate.now());

        //  获取指定字段的值
        System.out.println(date1.get(ChronoField.YEAR));
        System.out.println(date1.get(ChronoField.MONTH_OF_YEAR));
        System.out.println(date1.get(ChronoField.DAY_OF_YEAR));
        System.out.println(date1.get(ChronoField.DAY_OF_MONTH));

        // 多了一些加减运算
        //Peroid是针对日期的 , Duration 主要是针对Time的
        System.out.println(date1.minus(Period.ofYears(2)));
        System.out.println(date1.minus(Period.ofDays(2)));
        System.out.println(date1.minus(Period.ofWeeks(2)));

        Period between = Period.between(date1.minus(Period.ofYears(2)), date1);
        Period between2 = Period.between(date1.minus(Period.ofMonths(2)), date1);

        System.out.println(between.getMonths());
        System.out.println(between2.getMonths());
        System.out.println("date1.minus(between) = " + date1.minus(between));

        LocalDate now = LocalDate.now();
        // 替换年
        System.out.println(now.withYear(2016));

        // 计算某一个时间字段的取值范围
        System.out.println(now.range(ChronoField.DAY_OF_MONTH));
    }

    @Test
    public void LocalTimeTest() {

        LocalTime now = LocalTime.now();
        System.out.println(now);

        LocalTime time = LocalTime.of(12, 12);
        System.out.println(time);

        System.out.println(time.isBefore(now));
        // 由于是time所以不支持年字段
        System.out.println(time.isSupported(ChronoField.YEAR));
        System.out.println(time.isSupported(ChronoUnit.YEARS));
        System.out.println(time.isSupported(ChronoUnit.HOURS));

        System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss")));

        // Duration 主要是针对Time的,Peroid是针对日期的
        System.out.println(" time.minus(Duration.ofHours(2)) = " + time.minus(Duration.ofHours(2)));
        System.out.println(" time.plus(Duration.ofHours(2)) = " + time.plus(Duration.ofHours(2)));

    }

    @Test
    public void LocalDateTimeTest() {

        LocalDateTime dateTime = LocalDateTime.now();
        //2018-12-29T13:38:03.212
        System.out.println(dateTime.toString());

        dateTime = LocalDateTime.of(2018, 12, 29, 12, 12);

        System.out.println(dateTime);

        //指定今天的12点12分

        LocalDateTime toDay1212 = LocalDate.now().atTime(LocalTime.of(12, 12));
        System.out.println(toDay1212);

        //DateTime转Date
        System.out.println(toDay1212.toLocalDate());
        //DateTime转Time
        System.out.println(toDay1212.toLocalTime());
        // 替换某一个时间单位
        System.out.println(toDay1212.withHour(14));

    }

    @Test
    public void instantTest() {

        System.out.println(Instant.now());
        System.out.println(System.currentTimeMillis());
        System.out.println(System.nanoTime());
        System.out.println(Instant.now().toEpochMilli());
        System.out.println(Instant.now().plusNanos(0));
        System.out.println(Instant.now().get(ChronoField.NANO_OF_SECOND));

        Period between = Period.between(LocalDate.of(2018, 12, 26), LocalDate.of(2018, 12, 28));

        System.out.println(between.getDays());
        // between表示是一个间隔,做加减法时候只拿间隔做运算,不考虑具体的起止日期
        System.out.println(LocalDateTime.now().plus(between));

    }

    @Test
    public void TemporalAdjusterTest() {

        // 计算当前日期的加一个周五
        System.out.println(LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
        // 计算该月的最后一天
        System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()));
        // 计算该年的最后一天
        System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()));

    }

    @Test
    public void DateTimeFormatterTest() {
        //DateTimeFormatter 线程安全线的 ,SimpleDateFormat线程不安全原因是底层公用一个Calender成员
        System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    }

    @Test
    public void ZoneIdTest() {

        System.out.println(ZoneId.systemDefault());

    }
}

  

原文地址:https://www.cnblogs.com/leodaxin/p/10195907.html

时间: 2024-08-04 01:21:56

Java8时间的简单时间的相关文章

【ThinkingInC++】48、用标准C库中的时间函数生成简单的Time类

Cpptime.h /** * 书本:[ThinkingInC++] * 功能:用标准C库中的时间函数生成简单的Time类 * 时间:2014年9月11日07:53:56 * 作者:cutter_point */ #ifndef CPPTIME_H_INCLUDED #define CPPTIME_H_INCLUDED #include<ctime> #include<cstring> //这里ctime和cstring是不包含using namespace std;的而后面加.h

Oracle 如何对时间进行简单加减运算

在我们用dbms_job包进行定时Job的时候,需要设置时间间隔,所以需要知道时间的基本加减方法. SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'; 会话已更改. SQL> select sysdate, sysdate+1/24, sysdate +1/1440, sysdate + 1/86400 from dual; --分别是加一小时,一分钟,一秒钟 SYSDATE SYSDATE+1/24 SYSDATE+

Python3.x:简单时间调度Timer(间隔时间执行)

Python3.x:简单时间调度Timer(间隔时间执行) 代码: import threading import time def fun_timer(): print('hello timer') global timer #重复构造定时器 timer = threading.Timer(5.8,fun_timer) timer.start() #定时调度 timer = threading.Timer(2,fun_timer) timer.start() # 50秒后停止定时器 time.

Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法:

---------------------------------------------------------------------------------------------------------------------------------------- QTime::QTime() 默认构造函数,构造一个时,分,秒都为0的时间,如00:00:00.000(午夜) QTime::QTime(int h, int m, int s=0, int ms = 0) 构造一个用户指定时

java8 新日期和时间demo

//LocalDateTime private static void test1() { //初始化日期和时间 LocalDateTime ldt1=LocalDateTime.of(2016,10,04,10,40,50); System.out.println(ldt1);//2016-10-04T10:40:50 //获取当前日期和时间 LocalDateTime ldt2=LocalDateTime.now(); System.out.println(ldt2);//2017-11-0

sqlite 时间函数及时间处理

SQLite分页显示:Select * From news order by id desc Limit 10 Offset 10这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么翻译不当的地方希望大家指出,毕竟我的英文水平实在很差. SQLite 包括以下五个时间函数: date(日期时间字符串, 修正符, 修正符, ……)time(日期时间字符串, 修正符, 修正符, ……)datetime(日期时间字符串, 修正符, 修正符, ……)julianday(日期时间字符串,

Centos- Nagios 的Last Check更新时间与当前时间差距分析问题及处理方法总结

故障现象: 2014年6月4日 收到客户邮件说:bjd nagios 的Last Check更新时间与当前时间差距很大 具体处理过程如下: 盲目处理阶段: 想将问题尽快处理掉,所以有点只看问题表象忽略了重点,唉,说多了都是泪. 查询该机器各种log 发现除了一些常规报错信息,没有重要发现. 检查机器磁盘空间,内存,IO,CPU正常. 此问题首次出现,之前未有遇到.通过查询资料得知是由于此文件权限发生变化导致.但是任我怎么修改文件的权限和所属组都不能解决问题.并参考了http://myhat.bl

mysql中TIMESTAMP设置默认时间为当前时间

在我们保存数据进入到数据库中时多半会使用像php之类的脚本来获取一个时间保存到mysql中,其实在mysql可以直接使用TIMESTAMP 数据类型来实现默认类型了,下面一起来看看. 很多时候,为了简单,我们在设计数据表的时候,都需要设置时间字段为当前时间.可是MySQL 中,默认值无法使用函数,也就是你无法设置某一列,默认值是 NOW () 这样的处理.那怎么办呢? TIMESTAMP 数据类型.插入的时候,忽略该列即可.  代码如下 复制代码 dt TIMESTAMP /*等价于*/dt T

Python 系统时间与Mysql时间对比

由于自己是负责海外项目,常常会遇到一些问题,最近被系统时间与mysql时间不在一个时区,而坑了自己,一般修改了系统时区之后,MySQL必须重启,不然MySQL时区是不对的,会导致数据全部都是错的~~~,哎,只有坑到了自己,才会想到要去避免这种事情再次出现,所以用python写了一个简单判断时区的脚本,时区不对并邮件发出来,大家参考参考,详情如下: 1.脚本实例 #!/usr/bin/env python # coding=utf8 # auther:kuangl # This is system