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


这样就可以方便直接的对日期进行加减并且直接取得当前日期,并且继承之前可以用的方法。



0 0