Java中日期处理
2006-12-04 12:23
393 查看
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.GregorianCalendar;
/*
G Era 标志符
y 年
M 年中的月份
w 年中的周数
W 月份中的周数
D 年中的天数
d 月份中的天数
F 月份中的星期
E 星期中的天数
a Am/pm 标记
H 一天中的小时数(0-23)
k 一天中的小时数(1-24)
K am/pm 中的小时数(0-11)
h am/pm 中的小时数(1-12)
m 小时中的分钟数
s 分钟中的秒数
S 毫秒数
z 时区
Z 时区
*/
public class SimpleDateFormatTest
{
/**
* 字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格式一致即可)
* parse
*/
public static void parse() throws Exception
{
//注意MM是月,mm是分钟,HH代表0-23,hh代表1-12 AM/PM
String dateStr = "10/23/2006 6:35:55";
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = formatter.parse(dateStr);
System.out.println(date);
}
/**
* 日期类型转换成字符串
* format
*/
public static void format() throws Exception
{
SimpleDateFormat formatter1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date1 = formatter1.parse("10/23/2006 6:35:55");
System.out.println(date1);
GregorianCalendar gc = new GregorianCalendar(2006,8,22,9,44,56);
Date date2 = gc.getTime();
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str1 = formatter2.format(date1);
String str2 = formatter2.format(date2);
System.out.println(str1);
System.out.println(str2);
}
/**
* 当前时间
*/
public static void nowDate()
{
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = gc.getTime();
String nowDateStr = formatter.format(nowDate);
System.out.println("now date : " + nowDateStr);
}
/**
* 1年前此时
*/
public static void yesteryear()
{
GregorianCalendar gc = new GregorianCalendar();
Date nowDate = gc.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowDateStr = formatter.format(nowDate);
System.out.println("now date: " + nowDateStr);
//此时自1970年1月1日00:00:00GMT以来此Date对象表示的毫秒数
long nowDateMS = nowDate.getTime();
//去年此时自1970年1月1日00:00:00GMT以来此Date对象表示的毫秒数
long yesteryearNowDateMS = (nowDateMS/1000 - 60*60*24*365)*1000;
Date yesteryearNowDate = new Date(yesteryearNowDateMS);
String yesteryearNowDateStr = formatter.format(yesteryearNowDate);
System.out.println("yesteryear date: " + yesteryearNowDateStr);
}
/**
* 明天此时
*/
public static void tomorrow()
{
//当前日期
GregorianCalendar gc = new GregorianCalendar();
Date nowDate = gc.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowDateStr = formatter.format(nowDate);
System.out.println("now date: " + nowDateStr);
//当前自1970.1.1 00:00:00GMT以来的毫秒数
long nowTime = nowDate.getTime();
//明天此时自1970.1.1 00:00:00GMT以来的毫秒数
long tomorrowTime = (nowTime/1000 + 60*60*24)*1000;
Date tommorDate = new Date(tomorrowTime);
String tomorrowThisTimeStr = formatter.format(tommorDate);
System.out.println("tomorrow this date: " + tomorrowThisTimeStr);
}
/**
* 两个时间之间的天数
*
*/
public static void interval() throws Exception
{
String strA = "2006-9-10";
String strB = "2006-9-16";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateA = formatter.parse(strA);
Date dateB = formatter.parse(strB);
long dateAMS = dateA.getTime();
long dateBMS = dateB.getTime();
//两日期相差的毫秒数
long intervalMS = dateBMS - dateAMS;
//两日期相差的天数
long intervalDay = intervalMS/1000/60/60/24;
System.out.println(strA + "与" + strB + "相隔" + intervalDay + "天");
}
/**
* 某年某月第几周求日期
* 如:2006年9月的第2个星期五是几号
*/
public static void calculatDate() throws Exception
{
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");
Date date2= formatter2.parse("2006-09 2 星期五");//2006年9月的第2个星期五是几号
SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");
String mydate2=formatter3.format(date2);
System.out.println(mydate2);
}
/**
* 给定日期求是星期几
*/
public static void calculatWeek() throws Exception
{
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter1.parse("2006-9-10");
SimpleDateFormat formatter2 = new SimpleDateFormat("E");
String weekStr = formatter2.format(date);
System.out.println(weekStr);
}
public static void main(String[] args) throws Exception
{
parse();
format();
nowDate();
yesteryear();
tomorrow();
interval();
calculatDate();
calculatWeek();
}
}
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.GregorianCalendar;
/*
G Era 标志符
y 年
M 年中的月份
w 年中的周数
W 月份中的周数
D 年中的天数
d 月份中的天数
F 月份中的星期
E 星期中的天数
a Am/pm 标记
H 一天中的小时数(0-23)
k 一天中的小时数(1-24)
K am/pm 中的小时数(0-11)
h am/pm 中的小时数(1-12)
m 小时中的分钟数
s 分钟中的秒数
S 毫秒数
z 时区
Z 时区
*/
public class SimpleDateFormatTest
{
/**
* 字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格式一致即可)
* parse
*/
public static void parse() throws Exception
{
//注意MM是月,mm是分钟,HH代表0-23,hh代表1-12 AM/PM
String dateStr = "10/23/2006 6:35:55";
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = formatter.parse(dateStr);
System.out.println(date);
}
/**
* 日期类型转换成字符串
* format
*/
public static void format() throws Exception
{
SimpleDateFormat formatter1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date1 = formatter1.parse("10/23/2006 6:35:55");
System.out.println(date1);
GregorianCalendar gc = new GregorianCalendar(2006,8,22,9,44,56);
Date date2 = gc.getTime();
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str1 = formatter2.format(date1);
String str2 = formatter2.format(date2);
System.out.println(str1);
System.out.println(str2);
}
/**
* 当前时间
*/
public static void nowDate()
{
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = gc.getTime();
String nowDateStr = formatter.format(nowDate);
System.out.println("now date : " + nowDateStr);
}
/**
* 1年前此时
*/
public static void yesteryear()
{
GregorianCalendar gc = new GregorianCalendar();
Date nowDate = gc.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowDateStr = formatter.format(nowDate);
System.out.println("now date: " + nowDateStr);
//此时自1970年1月1日00:00:00GMT以来此Date对象表示的毫秒数
long nowDateMS = nowDate.getTime();
//去年此时自1970年1月1日00:00:00GMT以来此Date对象表示的毫秒数
long yesteryearNowDateMS = (nowDateMS/1000 - 60*60*24*365)*1000;
Date yesteryearNowDate = new Date(yesteryearNowDateMS);
String yesteryearNowDateStr = formatter.format(yesteryearNowDate);
System.out.println("yesteryear date: " + yesteryearNowDateStr);
}
/**
* 明天此时
*/
public static void tomorrow()
{
//当前日期
GregorianCalendar gc = new GregorianCalendar();
Date nowDate = gc.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowDateStr = formatter.format(nowDate);
System.out.println("now date: " + nowDateStr);
//当前自1970.1.1 00:00:00GMT以来的毫秒数
long nowTime = nowDate.getTime();
//明天此时自1970.1.1 00:00:00GMT以来的毫秒数
long tomorrowTime = (nowTime/1000 + 60*60*24)*1000;
Date tommorDate = new Date(tomorrowTime);
String tomorrowThisTimeStr = formatter.format(tommorDate);
System.out.println("tomorrow this date: " + tomorrowThisTimeStr);
}
/**
* 两个时间之间的天数
*
*/
public static void interval() throws Exception
{
String strA = "2006-9-10";
String strB = "2006-9-16";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateA = formatter.parse(strA);
Date dateB = formatter.parse(strB);
long dateAMS = dateA.getTime();
long dateBMS = dateB.getTime();
//两日期相差的毫秒数
long intervalMS = dateBMS - dateAMS;
//两日期相差的天数
long intervalDay = intervalMS/1000/60/60/24;
System.out.println(strA + "与" + strB + "相隔" + intervalDay + "天");
}
/**
* 某年某月第几周求日期
* 如:2006年9月的第2个星期五是几号
*/
public static void calculatDate() throws Exception
{
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");
Date date2= formatter2.parse("2006-09 2 星期五");//2006年9月的第2个星期五是几号
SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");
String mydate2=formatter3.format(date2);
System.out.println(mydate2);
}
/**
* 给定日期求是星期几
*/
public static void calculatWeek() throws Exception
{
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter1.parse("2006-9-10");
SimpleDateFormat formatter2 = new SimpleDateFormat("E");
String weekStr = formatter2.format(date);
System.out.println(weekStr);
}
public static void main(String[] args) throws Exception
{
parse();
format();
nowDate();
yesteryear();
tomorrow();
interval();
calculatDate();
calculatWeek();
}
}
相关文章推荐
- java日期处理
- java中的日期时间处理
- java日期的一些处理
- java处理日期,在指定日期的基础上加5天,感觉似乎java处理日期比C#复杂很多
- java常用类-java日期处理类
- 关于java的日期处理
- java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类
- java日期处理(四)
- java日期处理(六)
- java日期处理类总结 转
- 【转】JAVA 日期处理小节
- JAVA 对日期处理
- 【Util】java处理日期时间 相加减大全
- java日期处理类DateUtil
- Java的几个实用小Util函数(日期处理和http)
- Java日期处理工具类DateUtils详解
- java日期处理总结
- Java日期处理常用方法
- java对时间日期的处理
- JAVA时间日期处理类,主要用来遍历两个日期之间的每一天。