码迷,mamicode.com
首页 > 编程语言 > 详细

Java - Calendar Date DateUtils SimpleDateFormat

时间:2014-10-10 00:11:58      阅读:447      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   java   for   sp   div   

 1 import java.text.DateFormat;
 2 import java.text.ParseException;
 3 import java.text.SimpleDateFormat;
 4 import java.util.Calendar;
 5 import java.util.Date;
 6 
 7 import org.apache.commons.lang.time.DateUtils;
 8 
 9 public class Test {
10 
11     public static void main(String[] args) throws ParseException {
12 
13         // 设置格式
14         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
15 
16         // String -> Date
17         String txtTodate = "2014-10-01 22:31:11";
18         Date now = sdf.parse(txtTodate);
19         System.out.println(now); // Wed Oct 01 22:31:11 GMT+08:00 2014
20 
21         // Date -> String
22         String dateTotxt = sdf.format(now);
23         System.out.println(dateTotxt); // 2014-10-01 22:31:11
24 
25         // Date -> Calendar
26         Calendar cnow = Calendar.getInstance();
27         cnow.setTime(now);
28         System.out.println(cnow); // java.util.GregorianCalendar[time=1412173871000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+08:00",offset=28800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=9,WEEK_OF_YEAR=40,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=274,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=31,SECOND=11,MILLISECOND=0,ZONE_OFFSET=28800000,DST_OFFSET=0]
29 
30         //Calendar -> date
31         now = cnow.getTime();
32         System.out.println(now);//Wed Oct 01 22:31:11 GMT+08:00 2014
33         
34         // DateUtils.truncate
35         System.out.println(sdf.format(DateUtils.truncate(now, Calendar.SECOND)));//2014-10-01 22:31:11
36         System.out.println(sdf.format(DateUtils.truncate(now, Calendar.MINUTE)));//2014-10-01 22:31:00
37         System.out.println(sdf.format(DateUtils.truncate(now, Calendar.HOUR_OF_DAY)));//2014-10-01 22:00:00
38         System.out.println(sdf.format(DateUtils.truncate(now, Calendar.DAY_OF_MONTH)));//2014-10-01 00:00:00
39         System.out.println(sdf.format(DateUtils.truncate(now, Calendar.MONTH)));//2014-10-01 00:00:00
40         System.out.println(sdf.format(DateUtils.truncate(now, Calendar.YEAR)));//2014-10-01 00:00:00
41         
42         //Calendar add
43         Calendar temp = (Calendar)cnow.clone();
44         temp.add(Calendar.DAY_OF_YEAR, 1);
45         System.out.println(sdf.format(temp.getTime()));//2014-10-02 22:31:11
46         temp.add(Calendar.DAY_OF_YEAR, -2);
47         System.out.println(sdf.format(temp.getTime()));//2014-10-02 22:31:11
48         
49         //Calendar set
50         temp.set(2010, 8, 4, 22, 15, 20); // 月份减 1
51         System.out.println(sdf.format(temp.getTime()));//2010-09-04 22:15:20
52         
53         Calendar temp2 = (Calendar)temp.clone();
54         temp2.add(Calendar.MONTH, 1);
55         
56         //Calendar diff
57         long timeMillis = ( temp2.getTimeInMillis()-temp.getTimeInMillis());
58         long timeDays = timeMillis/1000/60/60/24;
59         System.out.println(timeDays);
60         
61         
62     }
63 
64 }

 

Java - Calendar Date DateUtils SimpleDateFormat

标签:style   blog   color   io   ar   java   for   sp   div   

原文地址:http://www.cnblogs.com/yaesuo/p/4014458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!