Java date format时间格式化操作示例
作者:ITshu 时间:2021-10-28 19:12:24
本文实例讲述了Java date format时间格式化操作。分享给大家供大家参考,具体如下:
import java.util.Date;
import java.text.DateFormat;
/**
* 格式化时间类
* DateFormat.FULL = 0
* DateFormat.DEFAULT = 2
* DateFormat.LONG = 1
* DateFormat.MEDIUM = 2
* DateFormat.SHORT = 3
* @author Michael
* @version 1.0
*/
public class Test{
public static void main(String []args){
Date d = new Date();
String s;
/** Date类的格式: Sat Apr 16 13:17:29 CST 2016 */
System.out.println(d);
System.out.println("******************************************");
/** getDateInstance() */
/** 输出格式: 2016-4-16 */
s = DateFormat.getDateInstance().format(d);
System.out.println(s);
/** 输出格式: 2016-4-16 */
s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
System.out.println(s);
/** 输出格式: 2016年4月16日 星期六 */
s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
System.out.println(s);
/** 输出格式: 2016-4-16 */
s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
System.out.println(s);
/** 输出格式: 06-4-16 */
s = DateFormat.getDateInstance(DateFormat.sHORT).format(d);
System.out.println(s);
/** 输出格式: 2016-01-01 00:00:00 */
java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
s = format1.format(new Date());
System.out.println(s);
/** 输出格式: 2016-01-01 01:00:00 */
System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()));
/** 输出格式: 2016-01-01 13:00:00 */
System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
/** 输出格式: 20160101000000***/
java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmss");
s = format2.format(new Date());
System.out.println(s);
}
}
补充一下:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
System.out.println("System Date: " + cal.get(Calendar.MONTH+1));
注意下,月份是从0开始的,要Calendar.MONTH+1才可以的。
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc
在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
更多java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《Java图片操作技巧汇总》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》及《Java数据结构与算法教程》。
希望本文所述对大家java程序设计有所帮助。
标签:Java,date,时间,格式化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java常量池知识点总结
2023-01-09 10:23:09
![](https://img.aspxhome.com/file/2023/2/66712_0s.png)
Spring Boot 配置和使用多线程池的实现
2022-09-04 19:53:02
Java中反射动态 代理接口的详解及实例
2023-11-28 05:29:36
![](https://img.aspxhome.com/file/2023/6/60266_0s.png)
详解使用Spring的BeanPostProcessor优雅的实现工厂模式
2023-01-14 02:15:53
深入了解Java核心类库--Math类
2023-08-19 01:06:21
springboot搭建访客管理系统的实现示例
2023-09-02 13:10:41
![](https://img.aspxhome.com/file/2023/3/58463_0s.png)
Java C++ 算法题解leetcode669修剪二叉搜索树示例
2022-09-22 04:56:13
![](https://img.aspxhome.com/file/2023/9/61119_0s.png)
java实现递归菜单树
2023-02-01 00:03:06
Mybatis Plus select 实现只查询部分字段
2022-07-23 18:51:32
![](https://img.aspxhome.com/file/2023/9/61279_0s.jpg)
列举java语言中反射的常用方法及实例代码
2022-10-31 13:45:07
C#中const,readonly和static关键字的用法介绍
2021-08-17 15:33:45
Java数组索引异常产生及解决方案
2023-11-05 16:52:27
![](https://img.aspxhome.com/file/2023/2/58702_0s.png)
关于Mybatis的@param注解及多个传参
2021-09-10 21:17:21
Java 中解决Unsupported major.minor version 51.0的问题
2022-07-22 03:53:08
![](https://img.aspxhome.com/file/2023/6/62516_0s.jpg)
Flutter加载图片的多样玩法汇总
2023-08-24 09:48:22
![](https://img.aspxhome.com/file/2023/1/108291_0s.png)
用Java实现简单画板功能
2023-01-19 08:17:10
![](https://img.aspxhome.com/file/2023/1/61501_0s.jpg)
深入剖析构建JSON字符串的三种方式(推荐)
2023-09-26 07:47:22
![](https://img.aspxhome.com/file/2023/6/68696_0s.png)
一文带你搞懂Java8的LocalDateTime
2023-11-10 02:02:22
![](https://img.aspxhome.com/file/2023/4/59234_0s.png)
Java虚拟机JVM性能优化(三):垃圾收集详解
2021-10-16 10:38:49
![](https://img.aspxhome.com/file/2023/5/61615_0s.png)
C++实现的O(n)复杂度内查找第K大数算法示例
2023-06-30 15:51:13