java日期处理总结
public class TestDate {
public static void main(String[] args) {
TestDate testdate = new TestDate();
testdate.getSystemCurrentTime();
testdate.getCurrentDate();
}
/**
* 获取系统当前时间
* System.currentTimeMillis()返回系统当前时间,结果为1970年1月1日0时0分0秒开始,到程序执行取得系统时间为止所经过的毫秒数
* 1秒=1000毫秒
*/
public void getSystemCurrentTime(){
System.out.println("---获取系统当前时间---");
System.out.println(System.currentTimeMillis());
}
public void getCurrentDate(){
System.out.println("---获取系统当前时间---");
//创建并初始化一个日期(初始值为当前日期)
Date date = new Date();
System.out.println("现在的日期是 = " + date.toString());
System.out.println("自1970年1月1日0时0分0秒开始至今所经历的毫秒数 = " + date.getTime());
}
}
|
字母
|
日期或时间元素
|
表示
|
示例
|
|
G
|
Era 标志符
|
AD
|
|
|
y
|
年
|
1996; 96
|
|
|
M
|
年中的月份
|
July; Jul; 07
|
|
|
w
|
年中的周数
|
27
|
|
|
W
|
月份中的周数
|
2
|
|
|
D
|
年中的天数
|
189
|
|
|
d
|
月份中的天数
|
10
|
|
|
F
|
月份中的星期
|
2
|
|
|
E
|
星期中的天数
|
Tuesday; Tue
|
|
|
a
|
Am/pm 标记
|
PM
|
|
|
H
|
一天中的小时数(0-23)
|
0
|
|
|
k
|
一天中的小时数(1-24)
|
24
|
|
|
K
|
am/pm 中的小时数(0-11)
|
0
|
|
|
h
|
am/pm 中的小时数(1-12)
|
12
|
|
|
m
|
小时中的分钟数
|
30
|
|
|
s
|
分钟中的秒数
|
55
|
|
|
S
|
毫秒数
|
978
|
|
|
z
|
时区
|
Pacific Standard Time; PST; GMT-08:00
|
|
|
Z
|
时区
|
-0800
|
public class TestDateFormat {
public static void main(String[] args) throws ParseException {
TestDateFormat tdf = new TestDateFormat();
tdf.dateFormat();
}
/**
* 对SimpleDateFormat类进行测试
* @throws ParseException
*/
public void dateFormat() throws ParseException{
//创建日期
Date date = new Date();
//创建不同的日期格式
DateFormat df1 = DateFormat.getInstance();
DateFormat df2 = new SimpleDateFormat("yyyy-MM-01 hh:mm:ss EE");
DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA); //产生一个指定国家指定长度的日期格式,长度不同,显示的日期完整性也不同
DateFormat df4 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒 EE", Locale.CHINA);
DateFormat df5 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss EEEEEE", Locale.US);
DateFormat df6 = new SimpleDateFormat("yyyy-MM-dd");
//将日期按照不同格式进行输出
System.out.println("-------将日期按照不同格式进行输出------");
System.out.println("按照Java默认的日期格式,默认的区域 : " + df1.format(date));
System.out.println("按照指定格式 yyyy-MM-dd hh:mm:ss EE ,系统默认区域 :" + df2.format(date));
System.out.println("按照日期的FULL模式,区域设置为中文 : " + df3.format(date));
System.out.println("按照指定格式 yyyy年MM月dd日 hh时mm分ss秒 EE ,区域为中文 : " + df4.format(date));
System.out.println("按照指定格式 yyyy-MM-dd hh:mm:ss EE ,区域为美国 : " + df5.format(date));
System.out.println("按照指定格式 yyyy-MM-dd ,系统默认区域 : " + df6.format(date));
//将符合该格式的字符串转换为日期,若格式不相配,则会出错
Date date1 = df1.parse("16-01-24 下午2:32");
Date date2 = df2.parse("2016-01-24 02:51:07 星期日");
Date date3 = df3.parse("2016年01月24日 星期五");
Date date4 = df4.parse("2016年01月24日 02时51分18秒 星期日");
Date date5 = df5.parse("2016-01-24 02:51:18 Sunday");
Date date6 = df6.parse("2016-01-24");
System.out.println("-------输出将字符串转换为日期的结果------");
System.out.println(date1);
System.out.println(date2);
System.out.println(date3);
System.out.println(date4);
System.out.println(date5);
System.out.println(date6);
}
}
public class TestCalendar {
public static void main(String[] args) throws ParseException {
TestCalendar testCalendar = new TestCalendar();
testCalendar.testCalendar();
testCalendar.testCalendar2();
}
public void testCalendar(){
//创建Calendar的方式
Calendar now1 = Calendar.getInstance();
Calendar now2 = new GregorianCalendar();
Calendar now3 = new GregorianCalendar(2016, 01, 24);
Calendar now4 = new GregorianCalendar(2016, 01, 24, 15, 55); //陷阱:Calendar的月份是0~11
Calendar now5 = new GregorianCalendar(2016, 01, 24, 15, 55, 44);
Calendar now6 = new GregorianCalendar(Locale.US);
Calendar now7 = new GregorianCalendar(TimeZone.getTimeZone("GMT-8:00"));
//通过日期和毫秒数设置Calendar
now2.setTime(new Date());
System.out.println(now2);
now2.setTimeInMillis(new Date().getTime());
System.out.println(now2);
//定义日期的中文输出格式,并输出日期
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒 E", Locale.CHINA);
System.out.println("获取日期中文格式化化输出:" + df.format(now5.getTime()));
System.out.println();
System.out.println("--------通过Calendar获取日期中年月日等相关信息--------");
System.out.println("获取年:" + now5.get(Calendar.YEAR));
System.out.println("获取月(月份是从0开始的):" + now5.get(Calendar.MONTH));
System.out.println("获取日:" + now5.get(Calendar.DAY_OF_MONTH));
System.out.println("获取时:" + now5.get(Calendar.HOUR));
System.out.println("获取分:" + now5.get(Calendar.MINUTE));
System.out.println("获取秒:" + now5.get(Calendar.SECOND));
System.out.println("获取上午、下午:" + now5.get(Calendar.AM_PM));
System.out.println("获取星期数值(星期是从周日开始的):" + now5.get(Calendar.DAY_OF_WEEK));
System.out.println();
System.out.println("---------通用星期中文化转换---------");
String dayOfWeek[] = {"", "日", "一", "二", "三", "四", "五", "六"};
System.out.println("now5对象的星期是:" + dayOfWeek[now5.get(Calendar.DAY_OF_WEEK)]);
System.out.println();
System.out.println("---------通用月份中文化转换---------");
String months[] = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
System.out.println("now5对象的月份是: " + months[now5.get(Calendar.MONTH)]);
}
public void testCalendar2() throws ParseException{
//获取当前月份的最大天数
Calendar cal = Calendar.getInstance();
int maxday=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int minday=cal.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(maxday);
//取当月的最后一天
DateFormat formatter3=new SimpleDateFormat("yyyy-MM-"+maxday);
System.out.println(formatter3.format(cal.getTime()));
//取当月的最后一天
DateFormat formatter4=new SimpleDateFormat("yyyy-MM-"+minday);
System.out.println(formatter4.format(cal.getTime()));
//求两个日期之间相隔的天数
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date beginDate= format.parse("2007-12-24");
java.util.Date endDate= format.parse("2007-12-25");
long day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
System.out.println("相隔的天数="+day);
//一年前的日期
java.text.Format formatter5=new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date todayDate=new java.util.Date();
long beforeTime=(todayDate.getTime()/1000)-60*60*24*365;
todayDate.setTime(beforeTime*1000);
String beforeDate=formatter5.format(todayDate);
System.out.println(beforeDate);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
System.out.println(formatter5.format(calendar.getTime()));
//当前星期的星期一和星期日
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
GregorianCalendar gregorianCalendar = new GregorianCalendar();
int dayInWeek = gregorianCalendar.get(Calendar.DAY_OF_WEEK);
int offset = 0;
if (dayInWeek == 1) {
// 星期天
offset = 6;
} else {
// 星期一至星期六
offset = dayInWeek - 2;
}
gregorianCalendar.add(GregorianCalendar.DAY_OF_MONTH, -offset);
String sday = dateFormat.format(gregorianCalendar.getTime());
gregorianCalendar.add(GregorianCalendar.DAY_OF_MONTH, 6);
String eday = dateFormat.format(gregorianCalendar.getTime());
System.out.println("这个星期的星期一:" + sday);
System.out.println("这个星期的星期天:" + eday);
}
}
三、总结
java日期处理总结的更多相关文章
- [转]Java日期时间使用总结
原文地址:http://lavasoft.blog.51cto.com/62575/52975/ 一.Java中的日期概述 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中 ...
- java日期操作大全
摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个: 取得指定月份的第一天与取得指定月份的最后一天 http ...
- Java日期时间使用总结
一.Java中的日期概述 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式都是非常复杂的问题. 在J ...
- Java 日期时间
Java 日期时间 标签 : Java基础 Date java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含 ...
- Java日期时间使用(转)
Java日期时间使用总结 转自:http://lavasoft.blog.51cto.com/62575/52975/ 一.Java中的日期概述 日期在Java中是一块非常复杂的内容,对于一个 ...
- Java日期的格式String类型GMT,GST换算成日期Date种类
请尊重他人的劳动成果.转载请注明出处:Java日期格式化之将String类型的GMT,GST日期转换成Date类型 http://blog.csdn.net/fengyuzhengfan/articl ...
- java日期格式大全 format SimpleDateFormat(转)
java日期格式大全 format SimpleDateFormat /** * 字符串转换为java.util.Date<br> * 支持格式为 yyyy.MM.dd G ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
- Java日期格式化方法
首先获取当前系统时间的方法有两种:第一种可以用currentTimeMillis()方法获取,它其实产生的是一个当前的毫秒数,这个毫秒是自1970年1月1日0时起至现在的毫秒数,类型是long 型,可 ...
随机推荐
- Hive variable demo
create table ori_trans (account string, maker string, tdate string) partitioned by (country string); ...
- Hanoi塔
2016-03-19 17:01:35 问题描述: 假设有三个命名为 A B C 的塔座 ,在塔座A上插有n个直径大小不相同,由小到大编号为1 ,2 ,3 ,··· ,n的圆盘,要求将A座上的圆盘移至 ...
- JS中的基本运动逻辑思想总结
总结一下自己今天学习运动的基本思想:‘ [1]对于移动的div,使其在某一个位置停止将其封装成一个函数,仅仅改变speed的正负即可 涉及到问题包括: var time=null; function ...
- .Net Framework 4.0 内部排序探索
简介 一时好奇心起,想一窥.Net Framework 4.0内部究竟是使用何种算法排序.以前听人说Framework内部是使用的快速排序,但究竟耳听为虚,眼见为实.主要通过JetBrains dot ...
- java 21 - 13 IO流之序列化和反序列化
序列化流:把对象按照流一样的方式存入文本文件或者在网络中传输.对象 -- 流数据(ObjectOutputStream) 构造方法:ObjectInputStream(InputStream in) ...
- Chrome 开发工具 Javascript 调试技巧
http://www.w3cplus.com/tools/dev-tips.html 一.Sources 面板介绍: Sources 面板分为左中右 3 部分左:Sources 当前页面加载的资源列表 ...
- 在PLSQL中不能使用中文作为查询条件查询数据
解决方法: 1.在oracle服务端的注册表中找到oracle-->key_oradb11g_home1,在右侧找到NLS_LANG,将其数值数据改为SIMPLIFIED CHINESE_CH ...
- MySQL数据备份小结
一 MySQL备份恢复总结: 1,备份所有库 2,分库备份 3,备份某库中的某表 4,备份某库中的多个表 5,分表备份 6,只备份表结构 7,只备份数据 二 MySQL备份恢复参数总结: -A 备份所 ...
- Virtualbox下Ubuntu与主机Win7共享文件夹
记下来,免得老google. 1. 在虚拟机设置里设置好win7的共享文件夹位置:如c:\share 2.确定ubuntu下需要共享的文件夹,如~/linuxshare.注意,此文件夹名字必须与win ...
- GEOS库学习之四:几何关系判断
原理上一篇已经介绍过了,这篇就直接进行程序练习 #include "geos.h" GeometryFactory factory; //创建一条环线,与线的区别就是环线是闭合的. ...