Date日期的格式与处理
来源:互联网 发布:错误推算法实例 编辑:程序博客网 时间:2024/06/08 15:36
最近对Date日期应用的比较多,然而之前对于Date的理解还不够深刻,所以写这个文档来记录一下Date。
首先Date在java中有两个类中有,一个是 java.util.Date 另一个是 java.sql.Date。
我们一般用的是第一种。
import java.util.Date;public class DateFormat { public static void main(String[] args) { Date currentDate=new Date(); System.out.println("currentDate is" +currentDate); }}
显示结果currentDate isWed Jun 04 19:57:18 CST 2014
然而我们用
而月则需要加1因为是从0开始计算的。Date来直接取得时间时,年会从2014-》114( 用这个方法获取年份时是从1900年开始计算的,因此当年份为2014时,得到的结果为114,所以如果要得到最终的年份,要再加上1900。)
显然直接用Date不是很方便,所以我们可以采用一个SimpleDateFormat的一个类。
public class SimpleDateFormat extends DateFormat
SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。
SimpleDateFormat 允许以为日期-时间格式化选择任何用户指定的方式启动。 但是,希望用 DateFormat 中的getTimeInstance、 getDateInstance 或 getDateTimeInstance 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 可以根据需要用 applyPattern 方法修改格式化方式。
SimpleDateFormat函数的继承关系:
java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
public class DateFormat { public static void main(String[] args) { Date currentDate=new Date(); SimpleDateFormat sdf=new SimpleDateFormat(); String time=sdf.format(currentDate); System.out.println("currentDate is " +time); Date currentTime=new Date(); SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-mm-dd"); String time2=sdf1.format(currentTime); System.out.println("currentDate is " +time2); }}
运行结果
currentDate is 14-6-4 下午8:10
currentDate is 2014-10-04
我们可以再新建一个simpleDateFormat的时候指定其格式,然后获取我们想要的日期格式。
但这样仅仅是解决了一个DateFormat的问题。
对于Date格式我们用得少了,更多的使用Calender
public class DateFormat { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd"); Calendar calendar=Calendar.getInstance(); String time=sdf.format(calendar.getTime()); System.out.println("currentDate is "+calendar.getTime()); System.out.println("currentDate is "+calendar.get(Calendar.YEAR)); System.out.println("currentDate is "+calendar.get(Calendar.MONTH)); System.out.println("currentDate is "+calendar.get(Calendar.DATE)); System.out.println("time is "+time); }}
输出结果
currentDate is Wed Jun 04 20:37:58 CST 2014
currentDate is 2014
currentDate is 5(输入的月份也需要加1)
currentDate is 4
time is 2014-37-04
月份是因为一月是0
如果有星期几的话需要减一,因为sunday是1,saturday是7
calender日期加减后赋值给Date类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String time=sdf.format(new Date()); Calendar cd = Calendar.getInstance(); try { cd.setTime(sdf.parse(time)); } catch (ParseException e) { e.printStackTrace(); } cd.add(Calendar.DATE, 1);//增加一天 //cal.add(Calendar.DATE, -1); //减一天 //cd.add(Calendar.MONTH, 1);//增加一月 Date date=cd.getTime(); System.out.println(sdf.format(date));
这是通过calendar来处理日期的增加减少。
总感觉这样对日期的处理也不是很好,我们选取了一个新的开源的Date处理库
Joda-Time
简介:
public class DateFormatshadow { public static void main(String[] args) throws ParseException { DateTime date=new DateTime(); DateTimeFormatter dtf=DateTimeFormat.forPattern("yyyy-MM-dd");//这地方注意一定要大写MM DateTime time=dtf.parseDateTime("2014-05-23"); //DateTime time=getDateTime("2011-05-22"); System.out.println(" "+date.toString()); System.out.println(" 年: "+date.getYear()); System.out.println(" 月: "+date.getMonthOfYear()); System.out.println(" 日: "+date.getDayOfMonth()); System.out.println(" "+time.toString()); System.out.print(time.plusDays(3).toString()); }}
输出结果
2014-06-04T21:31:36.112+08:00
年: 2014
月: 6
日: 4
2014-05-23T00:00:00.000+08:00
2014-05-26T00:00:00.000+08:00
这样就可以方便直接的对日期进行加减并且直接取得当前日期,并且继承之前可以用的方法。
- Date日期的格式与处理
- mongoimport 按json格式导入,date 日期 $date 的处理
- new Date() 日期格式处理
- Java日期格式--字符串与Date格式的转换
- js的date日期格式
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- java中string与date(日期)格式之间的转换
- Gson使用中遇到的Date格式问题。日期格式处理
- Gson使用中遇到的Date格式问题。日期格式处理
- 日期格式的处理
- // 处理日期的格式
- freemarker页面String类型的日期格式,实体类Date类型的----------处理方法
- java 获取date日期的String格式
- JQuery返回Json日期格式的問題 jquery对时间/Date(1387900800000)进行处理
- swift 笔记2
- 用了{dede:include filename="footer.htm"/}的问题-空行,在ie8中显示空文字
- android 解决ScrollView中嵌套ListView的冲突
- hdu 2528 线段交点
- android 调用系统api拍照遇到的一些问题
- Date日期的格式与处理
- 1787: [Ahoi2008]Meet 紧急集合
- getContextPath、getServletPath、getRequestURI的区别
- 做一个上进的人
- java IntelliJ IDEA 13 注册码
- Ajax系列之一些你应该知道的事
- 演示boost::asio::deadline_timer如何使用
- <jsp:directive.page> <%@ page import=""/>的用法和解释
- 【LeetCode】Length of Last Word