Date类型与格式字符串、毫秒的转化
2013-07-09 17:45
399 查看
1.JAVA日期加减运算2007-10-14 15:271.用java.util.Calender来实现
2.用java.text.SimpleDateFormat和java.util.Date来实现
Date对象实质上是一个日期转化成的毫秒数
1.
日期类型转化为毫秒数
Date d = new Date();
long m = d.getTime();
毫秒数转日期类型
Date dd = new Date(m) ;//通过毫秒数构造Date对象
2.
把年月日格式字符串转化为Date类型 ,用的是SimpleDateFormat对象的parse方法
把Date类型转化为年月日格式字符串类型,用的是SimpleDateFormat对象的format方法
Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天的日期 calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加1 System.out.println(calendar.get(Calendar.DATE));//加1之后的日期Top
2.用java.text.SimpleDateFormat和java.util.Date来实现
Date d=new Date(); SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); System.out.println("今天的日期:"+df.format(d)); System.out.println("两天前的日期:" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000))); System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000))); 注意SimpleDateFormat真正format的是Date数据 而将文本数据解析成日期对象 import java.text.SimpleDateFormat; import java.util.Date; public class DateExample3 { public static void main(String[] args) { // Create a date formatter that can parse dates of // the form MM-dd-yyyy. SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy"); // Create a string containing a text date to be parsed. String dateStringToParse = "9-29-2001"; try { // Parse the text version of the date. // We have to perform the parse method in a // try-catch construct in case dateStringToParse // does not contain a date in the format we are expecting. Date date = bartDateFormat.parse(dateStringToParse); // Now send the parsed date as a long value // to the system output. System.out.println(date.getTime()); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }
Date对象实质上是一个日期转化成的毫秒数
1.
日期类型转化为毫秒数
Date d = new Date();
long m = d.getTime();
毫秒数转日期类型
Date dd = new Date(m) ;//通过毫秒数构造Date对象
2.
把年月日格式字符串转化为Date类型 ,用的是SimpleDateFormat对象的parse方法
把Date类型转化为年月日格式字符串类型,用的是SimpleDateFormat对象的format方法
相关文章推荐
- 字符串date类型格式时间串转化
- string datdString='Wed Oct 12 2016 00:00:00 GMT+0800 (中国标准时间)'将字符串转化为date类型,格式2016-10-12
- 将未指定格式的日期字符串转化成java.util.Date类型日期对象
- 3.利用SimpleDateFormat()把字符串格式的时间转化为Date类型的时间
- 将未指定格式的日期字符串转化成java.util.Date类型日期对象
- java 字符串转化成Date类型,校验格式
- 这篇很简单,只是一个新手发神经而已。关于oracle的date类型的格式转化。
- 时间格式字符串转化为date和时间戳
- java 如何将日期类型Date 时间戳 转换为 期望格式的 日期字符串(mongo db)
- yx:CST格式的字符串转化为Date对象
- java ------SimpleDateFormat------ 实现字符串与日期类型互相转化
- Oracle中日期Date类型格式的转化
- Java 日期格式是“Fri Feb 26 16:33:08 CST 2016”字符串类型,转date类型。java.util.date与sql.date的相互转换
- Date类型与毫秒值之间的转化,dateformat的解析和格式化,SimpleDateFormat的解析和格式化
- java中如何将String类型的日期格式为yyyyMMdd转化为date类型的yyyy-MM-dd格式
- java字符串转化为date类型
- Android开发之--将时间字符串转化成Date类型或反转
- 字符串转化成DATE类型
- JSON对象反序列化为Java对象的时候自定义Date类型的字符串格式
- 时间格式字符串转化为date和时间戳