Titan笔记

  • 首页
  • Java
  • 数据结构
  • Web
  • C语言
  • Python
  • 杂谈
  • 逸笔挥墨
Titan笔记
分享学习、研究与开发过程中的点滴记忆
  1. 首页
  2. Java
  3. 正文

[Java] 日期与时间的一些操作

2020年4月2日 320点热度 1人点赞 0条评论
实现构造一个时间,获取当前时间戳,日期时间与时间戳的互换等操作
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

// Java时间操作的一些实例 By Titan 2020-04-01
public class TimeStudy {
    public static void main(String[] args) {
        formatDate();
        dateCovert();
    }

    // 格式化当前时间
    public static void formatDate() {
        System.out.println("Format Date");
        // 老API方式
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println("The formatted date is : ");
        System.out.println(sdf.format(date));
        // Java8方式
        LocalDateTime lt = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("The formatted date is : ");
        System.out.println(formatter.format(lt));
        System.out.println("-------------------------------------------------------");
    }

    // 时间戳与时间日期的互相转换
    public static void dateCovert() {
        LocalDateTime lt = LocalDateTime.now();
        // 获取当前时间戳
        // 秒级时间戳
        long timestamp = lt.toEpochSecond(ZoneOffset.ofHours(8));
        // 毫秒级时间戳
        long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println("The current timestamp is: \n" + timestamp);
        System.out.println("The current millisecond timestamp is: \n" + milliSecond);
        // 获取任意时间的时间戳
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime newLt = LocalDateTime.parse("2019-01-01 00:00:00", formatter);
        long millSecond2 = newLt.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println("The mill-second of 2019-01-01 00:00:00 is:\n" + millSecond2);
        // 时间戳转时间
        Instant ins = Instant.ofEpochMilli(millSecond2);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(ins, ZoneOffset.ofHours(8));
        String dateTimeText = localDateTime.format(formatter);
        System.out.println("The LocalDateTime of " + millSecond2 + " is:\n" + dateTimeText);
        System.out.println("-------------------------------------------------------");

    }


}
结果类似于下面

JDK8中的新特性:用 LocalDateTime 与 DateTimeFormatter 代替了老的 Date 与 SimpeDateFormat,更加的方便,进行时间日期的运算也更加简洁了。
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: Java Java时间日期
最后更新:2020年4月2日

Titan

兴趣广泛而无一精擅
想到什么,我总是渴望以代码的方式去呈现
永远年轻,永远热泪盈眶
Stay Hungry, Stay Foolish

点赞
< 上一篇
下一篇 >

文章评论

取消回复

Titan

兴趣广泛而无一精擅
想到什么,我总是渴望以代码的方式去呈现
永远年轻,永远热泪盈眶
Stay Hungry, Stay Foolish

逸笔挥墨 - Titan的文学天地
文章分类
  • C语言 (4)
  • Hadoop (1)
  • Hive (3)
  • Java (19)
  • JavaWeb (4)
  • Linux运维之道 (1)
  • Mybatis学习笔记 (3)
  • Python (3)
  • SpringCloud (4)
  • Web (5)
  • Web前端 (4)
  • Web后端 (5)
  • 数据库 (1)
  • 数据结构 (10)
  • 杂谈 (3)
  • 诗词歌赋 (1)
  • 随摘 (2)
最新 热点 随机
最新 热点 随机
Spring Cloud 微服务学习笔记 - 负载均衡服务调用 Spring Cloud 微服务学习笔记 - Eureka 服务注册与发现 Spring Cloud 微服务学习笔记 - IDEA工程搭建 关于我和Titan笔记 Spring Cloud 微服务学习笔记 - 开篇 TitanEMS - Titan企业员工管理系统 - JavaWeb期末实践项目
Spring Cloud 微服务学习笔记 - 开篇TitanEMS - Titan企业员工管理系统 - JavaWeb期末实践项目2021年1月随摘2021年1月诗摘关于我和Titan笔记《梦之浮桥》中的几句
[PHP] Laravel框架介绍、安装及配置 (一)Mybatis的入门 (二) 使用Mybatis完成CRUD操作 Linux 网络优化指南 - 改善Linux的网络性能 [杂谈] 什么是递归函数? Titan商店 - 又一个Web静态项目
标签聚合
Java 二叉树 JavaWeb 链式存储 Python Apache-Hive Mybatis学习笔记 数据结构
友情链接
  • Mttblog

COPYRIGHT © 2016 - 2021 Titan笔记. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

豫ICP备20001822号-1

豫公网安备 41010502004418号