Java 格式化日期、时间
Java提供了Date、Calendar两个类用于处理日期、时间。
但Date的大部分构造器、方法已经过时,不在推荐使用,Calendar类又过于复杂,所以Java8推出了一套新的时间日期库。
1、Date类
Date常用的构造函数:
Date() 生成一个代表当前日期时间的Date对象,相当于Date(System.currentTimeMillis())
Date(long date) 指定时间戳,默认单位ms。距1970.1.1 00:00:00的毫秒数。
Date常用方法:
boolean after(Date date) 判断是否在该时间日期之后
boolean before(Date date)
long getTime() 获取时间戳
void setTime() 设置时间戳
少用Date,尽量用Calendar代替。
2、Calendar类
Calendar类常用方法:
Calendar calendar=Calendar.getInstance(); 使用静态方法获取实例,默认为当前的时间日期
calendar.get(Calendar.YEAR) 获取指定字段的值,参数为预定义的常量,返回值均为int。月份比较特殊,0-11,0表示1月。
calendar.set(Calendar.YEAR,2020); 设置指定字段的值 calendar.set(2020,1,1); 同时设置年月日
calendar.set(2020,1,1,1,1,1); 同时设置年月日时分秒 calendar.add(Calendar.YEAR,2); 增加指定字段的值 calendar.add(Calendar.YEAR,-1); 可以为负数 参数、返回值均为int型。
Calendar类示例:
package test; import java.util.Calendar; //需要实现Cloneable接口
public class Test{
public static void main(String[] args) {
//使用静态方法获取实例,默认为当前的时间日期
Calendar calendar=Calendar.getInstance();
//获取年份,打YEAR选择即可
System.out.println(calendar.get(Calendar.YEAR));
//获取月份,月份从0-11,0表示1月
System.out.println(calendar.get(Calendar.MONTH));
//获取日
System.out.println(calendar.get(Calendar.DATE));
//获取时
System.out.println(calendar.get(Calendar.HOUR));
//设置指定字段的值
calendar.set(Calendar.YEAR,2020);
//同时设置年月日
calendar.set(2020,1,1);
//同时设置年月日时分秒
calendar.set(2020,1,1,1,1,1);
//增加指定字段的值,年份+1
calendar.add(Calendar.YEAR,1);
//可以为负数,年份-1
calendar.add(Calendar.YEAR,-1);
}
}
3、Java8新增的时间日期包
LocalDate类:代表当前时区的日期
LocalTime类:代表当前时区的时间
LocalDateTime:代表当前时区的日期时间
以上三个类都提供了静态方法now()获取当前的时间/日期/时间日期对象。
示例:
package test;
import java.time.LocalDateTime;
public class Test{
public static void main(String[] args){
//使用静态方法now()获取当前时间日期
LocalDateTime ldt=LocalDateTime.now();
//使用plusXxx(int x)增加指定字段的值
ldt.plusDays(1); //Day字段的值+1
//使用minusXxx(int x)方法减少指定的字段的值
ldt.minusHours(1); //Day字段的值-1
}
}
有三种方法可以格式化日期、时间。
1、使用DateFormat类
获取DateFormat实例:
DateFormat.getDateInstance() 只能格式化日期 2019年5月13日
DateFormat.getTimeInstance() 只能格式化时间 下午10:06:07
DateFormat.getDateTimeInstance() 格式化日期时间 2019年5月13日 下午10:06:07
以上方法均为静态方法。
以上方法均有2个重载方法:
指定显示样式,DateFormat类预定义的常量。
DateFormat.getDateInstance(int style) 指定日期显示样式
DateFormat.getTimeInstance(int style) 指定时间显示样式
DateFormat.getDateTimeInstance(int style,int style) 第一个参数指定Date显示样式,第二个参数指定Time显示样式
可用常量:
DateForm.SHORT 2019/5/13 下午10:16
DateForm.MEDIUM 2019年5月13日 下午10:17:06 缺省时默认值就是DateForm.MEDIUM
DateForm.LONG 这个不常用
DateForm.FULL 2019年5月13日星期一 中国标准时间 下午10:18:44
以上三个方法可再加一个参数指定国家:
DateFormat.getDateInstance(int style,Locale.CHINA) 指定日期显示样式
DateFormat.getTimeInstance(int style,Locale.CHINA) 指定时间显示样式
DateFormat.getDateTimeInstance(int style,int style,Locale.CHINA) 第一个参数指定Date显示样式,第二个参数指定Time显示样式
也可以使用其他国家。缺省第二个参数时,会使用默认值。第二个参数一般可以缺省。
DateFormat实例常用的方法:
String format(Date date) 将Date对象转换为格式化的字符串,并返回字符串
package test; import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date; public class Test{
public static void main(String[] args) throws ParseException {
//使用静态方法获取实例。只能格式化日期
DateFormat df1=DateFormat.getDateInstance();
//只能格式化时间
DateFormat df2=DateFormat.getTimeInstance();
//格式化日期时间
DateFormat df3=DateFormat.getDateTimeInstance();
//要格式化的Date对象
Date date=new Date();
//使用format()格式化Date对象
System.out.println(df1.format(date));
System.out.println(df2.format(date));
System.out.println(df3.format(date));
}
}
2、使用SimpleDateForm类
SimpleDateForm类是DateForm类的子类。SimpleDateForm比DateForm更简单,功能更强大。
示例:
package test; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class Test{
public static void main(String[] args) throws ParseException {
//创建SimpleDateFormat对象,指定样式 2019-05-13 22:39:30
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//2019年的第133天。占位符是特定的
SimpleDateFormat sdf2=new SimpleDateFormat("y年的第D天");
//要格式化的Date对象
Date date=new Date();
//使用format()方法格式化Date对象为字符串,返回字符串
System.out.println(sdf1.format(date));
System.out.println(sdf2.format(date));
}
}
占位符:
G "公元"
y 四位数年份
M 月
d 日
h 时 在上午或下午 (1~12)
H 时 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 一周中的周几
D 一年中的第几天
w 一年中第几个星期
a 上午 / 下午 标记符
k 时(1~24)
K 时 在上午或下午 (0~11)
DateFormat只能使用特定的日期时间格式,SimpleDateFormat是自定义日期时间格式。
3、使用Java8新增的DateTimeFormatter类
DateTimeFormatter相当于DateFormat、SimpleDateFormat的合体,可以使用预定义的日期时间格式,也可以使用自定义的格式。但使用方式有些不同。
package test; import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle; public class Test{
public static void main(String[] args) throws ParseException {
//使用预定义的格式。和DateFormat不同,以下三个方法方法均没有重载方法,只能这样用。预定义的常量为FormatStyle类的SHORT、MEDIUM、LONG、FULL
DateTimeFormatter df1=DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); //只能格式化日期部分
DateTimeFormatter df2=DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT); //只能格式化时间部分
DateTimeFormatter df3=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT,FormatStyle.SHORT); //格式化日期时间 //使用自定义的格式
DateTimeFormatter df4=DateTimeFormatter.ofPattern("y-M-d H:m:s"); //要格式化的时间日期对象,只能用LocalDateTime。只涉及到日期也可以用LocalDate类,只涉及时间也可以用LocalTime类
LocalDateTime ldt=LocalDateTime.now(); //格式化,不能用Date类的实例作为参数
System.out.println(df1.format(ldt));
System.out.println(df2.format(ldt));
System.out.println(df3.format(ldt));
System.out.println(df4.format(ldt));
}
}
Java 格式化日期、时间的更多相关文章
- 【转】JAVA 8 日期/时间(Date Time)API指南
前言 本来想写下Java 8的日期/时间API,发现已经有篇不错的文章了,那就直接转载吧~ PS:主要内容没变,做了部分修改. 原文链接: journaldev 翻译: ImportNew.com - ...
- Java 8 日期时间 API
转自:https://www.runoob.com/java/java8-datetime-api.html Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与 ...
- Java 8 新特性-菜鸟教程 (8) -Java 8 日期时间 API
Java 8 日期时间 API Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理. 在旧版的 Java 中,日期时间 API 存在诸多问题,其中有: ...
- Java 8 日期时间API
Java 8一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期; 在介绍本篇文章内容之前,我们先来讨论Jav ...
- Java 8——日期时间工具库(java.time)
一.前言 在介绍Java SE 8中新的日期时间库前,先了解下Java 8之前的日期时间工具的诟病. 在Java SE 8前,日期时间工具库在java.util包中,包括: java.util.Dat ...
- Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例
Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...
- 格式化日期时间字符串 Get-Date -Uformat , -format
#将字符串格式化为时间格式 $dateTimeStr = '20141231T23:59:59' $format = 'yyyyMMddTHH:mm:ss' $formatProvider = [Gl ...
- IOS开发之格式化日期时间
IOS开发之格式化日期时间(转) 在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理. 例如: //实例化一个NSDateFor ...
- Smarty 获取当前日期时间和格式化日期时间
在Smarty 中获取当前日期时间和格式化日期时间与PHP中有些不同的地方,这里就为您详细介绍: 首先是获取当前的日期时间:在PHP中我们会使用date函数来获取当前的时间,实例代码如下:date(& ...
随机推荐
- maven项目中更新了核心库后导致一些包提示未定义,如:The import org.json cannot be resolved
经查看发现了原因,因为核心库的版本没有变更,本地仓库只更新了核心库的jar部分的库,没有更新核心库pom项目.从而导致了一些库的引用提示未定义. 以后有新库更新,最好在本地库删除全部的相关库,再mav ...
- 【451】python 同一行打印进度条
参考:Python3 Print 同一行打印显示进度条效果 参考:\r\n, \r and \n what is the difference between them? [duplicate] 参考 ...
- light4j一个轻量级的低延时、高吞吐量、内存占用量小的API平台
1.背景(abstract) 笔者算是一个极客类型的程序员了.喜欢探索一些程序内在的原理.稳定性.自动化运维.健壮性,很多时间也会 去对程序的内存使用率.cpu使用率锱铢必较.尽量克扣掉不必要的cpu ...
- LwIP应用开发笔记之四:LwIP无操作系统TFTP服务器
前面我们已经实现了UDP的回环客户端和回环服务器的简单应用,接下来我们实现一个基于UDP的简单文件传输协议TFTP. 1.TFTP协议简介 TFTP是TCP/IP协议族中的一个用来在客户机与服务器之间 ...
- hbase 安装(集群模式)
环境:jdk 1.8 + hadoop2.7.6+zookeeper3.4.9+centos7 一.安装zookeeper(集群模式) 0.安装机器 ip ...
- idea 添加自定义的todo标签
背景:idea添加自定义的todo标签可以提高开发效率,搞之 在idea定义个人风格的todo IDEA自定义TODO注释 主要分为如下两步 自定义todo标签 settings>Editor& ...
- activiti学习3:流程引擎对象和流程引擎配置对象
目录 activiti学习3:流程引擎对象和流程引擎配置对象 一.activiti的简单使用流程 二.流程引擎配置对象ProcessEngineConfiguration的介绍 三.activiti配 ...
- [转帖]如何查看windows某个目录下所有文件/文件夹的大小?
如何查看windows某个目录下所有文件/文件夹的大小? https://www.cnblogs.com/gered/p/10208281.html 挺好的工具 linux 上面 我就是使用 du - ...
- AntDesign vue学习笔记(一)初始化项目
最近学习AntDesign组件使用,官方Pro例子集成度太高,不容易学习,将从最基础组件一个一个搭建. 1.创建Vue Cli项目 2.引入ant design组件 $ cnpm i --save a ...
- window 关机
schtasks /create /tn "关机" /tr "shutdown /s" /sc once /st 20:30