时间util Calendar

来源:互联网 发布:大街上放的网络歌曲 编辑:程序博客网 时间:2024/05/01 03:23

/**
     * 
     * 取得当前日期毫秒数
     * 
     * @return long
     */
    public static long getTimeInMillis(){
    Calendar cal=Calendar.getInstance(); 
    //cal.setTimeZone(TimeZone.getTimeZone("GMT+0800"));
    return cal.getTimeInMillis();
    }


 

 /**
     * 
     * 根据格式化字符显示当前日期
     * 
     * @param formate 转化的格式
     * 
     * @return String
     */

    public static String formatDate(String format) {

//将从格林威治时间开始到当前时间的毫秒数转化成format格式,时区加8(北京时间)

        String date = DateFormatUtils.format(getTimeInMillis(), format,TimeZone.getTimeZone("GMT+0800"));
        return date;
    }




0

关于Calendar的getTimeInMillis()方法 

程序如下: 
import java.util.Calendar; 

public class TestCalendar { 
    public static void main(String[] args) { 
        Calendar calendar = Calendar.getInstance(); 
        calendar.clear(); 
        calendar.set(1970, 0, 1); 
        System.out.println(calendar.getTime()); 
        System.out.println(calendar.getTimeInMillis()); 
    } 

执行结果: 
Thu Jan 01 00:00:00 CST 1970 
-28800000 
1970-01-01 

理想结果: 
Thu Jan 01 00:00:00 CST 1970 

1970-01-01 
问题:calendar.getTimeInMillis()返回的是 
“当前时间,以从历元至现在所经过的 UTC 毫秒数形式。” 
这里的当前时间就是Thu Jan 01 00:00:00 CST 1970吧,它距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的毫秒数应该是0啊。怎么是-28800000这个数字呢
 




按时间排序按投票排序

00

采纳的答案

calendar.clear(); 
calendar.set(1970, 0, 1); 

TimeZone tz = TimeZone.getTimeZone("GMT");        
calendar1.setTimeZone(tz); 
System.out.println(calendar.getTime()); 
System.out.println(calendar.getTimeInMillis()); 

默认时区不对.



J











java Calendar 类时间操作,这也许是创建日历和管理最简单的一个方案,示范代码很简单。

演示了获取时间,日期时间的累加和累减,以及比较。


注意事项:

Calendar 的 month 从 0 开始,也就是全年 12 个月由 0 ~ 11 进行表示。

而 Calendar.DAY_OF_WEEK 定义和值如下:

Calendar.SUNDAY = 1
Calendar.MONDAY = 2
Calendar.TUESDAY = 3
Calendar.WEDNESDAY = 4
Calendar.THURSDAY = 5
Calendar.FRIDAY = 6
Calendar.SATURDAY = 7


SimpleDateFormat 的格式定义

LetterDate or Time ComponentPresentationExamplesGEra designatorTextADyYearYear199696YWeek yearYear200909MMonth in year (context sensitive)MonthJulyJul07LMonth in year (standalone form)MonthJulyJul07wWeek in yearNumber27WWeek in monthNumber2DDay in yearNumber189dDay in monthNumber10FDay of week in monthNumber2EDay name in weekTextTuesdayTueuDay number of week (1 = Monday, ..., 7 = Sunday)Number1aAm/pm markerTextPMHHour in day (0-23)Number0kHour in day (1-24)Number24KHour in am/pm (0-11)Number0hHour in am/pm (1-12)Number12mMinute in hourNumber30sSecond in minuteNumber55SMillisecondNumber978zTime zoneGeneral time zonePacific Standard TimePSTGMT-08:00ZTime zoneRFC 822 time zone-0800XTime zoneISO 8601 time zone-08-0800-08:00


Java Calendar 演示代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package demo;  
  2.   
  3. import java.util.Date;  
  4. import java.text.SimpleDateFormat;  
  5. import java.text.DateFormat;  
  6. import java.text.ParseException;  
  7. import java.util.Calendar;  
  8.   
  9. public class Test  
  10. {  
  11.   public Test()  
  12.   {  
  13.   }  
  14.   
  15.   public static void main(String[] args)  
  16.   {  
  17.     // 字符串转换日期格式  
  18.     // DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  19.     // 接收传入参数  
  20.     // String strDate = args[1];  
  21.     // 得到日期格式对象  
  22.     // Date date = fmtDateTime.parse(strDate);  
  23.   
  24.     // 完整显示今天日期时间  
  25.     String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());  
  26.     System.out.println(str);  
  27.   
  28.     // 创建 Calendar 对象  
  29.     Calendar calendar = Calendar.getInstance();  
  30.   
  31.     try  
  32.     {  
  33.       // 对 calendar 设置时间的方法  
  34.       // 设置传入的时间格式  
  35.       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");  
  36.       // 指定一个日期  
  37.       Date date = dateFormat.parse("2013-6-1 13:24:16");  
  38.       // 对 calendar 设置为 date 所定的日期  
  39.       calendar.setTime(date);  
  40.   
  41.       // 按特定格式显示刚设置的时间  
  42.       str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
  43.       System.out.println(str);  
  44.     }  
  45.     catch (ParseException e)  
  46.     {  
  47.       e.printStackTrace();  
  48.     }  
  49.   
  50.     // 或者另一種設置 calendar 方式  
  51.     // 分別爲 year, month, date, hourOfDay, minute, second  
  52.     calendar = Calendar.getInstance();  
  53.     calendar.set(201312173544);  
  54.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
  55.     System.out.println(str);  
  56.   
  57.     // Calendar 取得当前时间的方法  
  58.     // 初始化 (重置) Calendar 对象  
  59.     calendar = Calendar.getInstance();  
  60.     // 或者用 Date 来初始化 Calendar 对象  
  61.     calendar.setTime(new Date());  
  62.   
  63.     // setTime 类似上面一行  
  64.     // Date date = new Date();  
  65.     // calendar.setTime(date);  
  66.   
  67.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
  68.     System.out.println(str);  
  69.   
  70.     // 显示年份  
  71.     int year = calendar.get(Calendar.YEAR);  
  72.     System.out.println("year is = " + String.valueOf(year));  
  73.   
  74.     // 显示月份 (从0开始, 实际显示要加一)  
  75.     int month = calendar.get(Calendar.MONTH);  
  76.     System.out.println("nth is = " + (month + 1));  
  77.   
  78.     // 本周几  
  79.     int week = calendar.get(Calendar.DAY_OF_WEEK);  
  80.     System.out.println("week is = " + week);  
  81.   
  82.     // 今年的第 N 天  
  83.     int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);  
  84.     System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);  
  85.   
  86.     // 本月第 N 天  
  87.     int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);  
  88.     System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));  
  89.   
  90.     // 3小时以后  
  91.     calendar.add(Calendar.HOUR_OF_DAY, 3);  
  92.     int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);  
  93.     System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);  
  94.   
  95.     // 当前分钟数  
  96.     int MINUTE = calendar.get(Calendar.MINUTE);  
  97.     System.out.println("MINUTE = " + MINUTE);  
  98.   
  99.     // 15 分钟以后  
  100.     calendar.add(Calendar.MINUTE, 15);  
  101.     MINUTE = calendar.get(Calendar.MINUTE);  
  102.     System.out.println("MINUTE + 15 = " + MINUTE);  
  103.   
  104.     // 30分钟前  
  105.     calendar.add(Calendar.MINUTE, -30);  
  106.     MINUTE = calendar.get(Calendar.MINUTE);  
  107.     System.out.println("MINUTE - 30 = " + MINUTE);  
  108.   
  109.     // 格式化显示  
  110.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
  111.     System.out.println(str);  
  112.   
  113.     // 重置 Calendar 显示当前时间  
  114.     calendar.setTime(new Date());  
  115.     str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
  116.     System.out.println(str);  
  117.   
  118.     // 创建一个 Calendar 用于比较时间  
  119.     Calendar calendarNew = Calendar.getInstance();  
  120.   
  121.     // 设定为 5 小时以前,后者大,显示 -1  
  122.     calendarNew.add(Calendar.HOUR, -5);  
  123.     System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
  124.   
  125.     // 设定7小时以后,前者大,显示 1  
  126.     calendarNew.add(Calendar.HOUR, +7);  
  127.     System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
  128.   
  129.     // 退回 2 小时,时间相同,显示 0  
  130.     calendarNew.add(Calendar.HOUR, -2);  
  131.     System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
  132.   }  
  133. }  


要计算时间差,可用 Calendar.getTimeInMillis() 取得两个时间的微秒级的时间差,再加以换算,即可获得相差的天数,代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 得微秒级时间差  
  2. long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis();  
  3. // 换算后得到天数  
  4. long day = val / (1000 * 60 * 60 * 24);  
0

关于Calendar的getTimeInMillis()方法 10

程序如下: 
import java.util.Calendar; 

public class TestCalendar { 
    public static void main(String[] args) { 
        Calendar calendar = Calendar.getInstance(); 
        calendar.clear(); 
        calendar.set(1970, 0, 1); 
        System.out.println(calendar.getTime()); 
        System.out.println(calendar.getTimeInMillis()); 
    } 

执行结果: 
Thu Jan 01 00:00:00 CST 1970 
-28800000 
1970-01-01 

理想结果: 
Thu Jan 01 00:00:00 CST 1970 

1970-01-01 
问题:calendar.getTimeInMillis()返回的是 
“当前时间,以从历元至现在所经过的 UTC 毫秒数形式。” 
这里的当前时间就是Thu Jan 01 00:00:00 CST 1970吧,它距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的毫秒数应该是0啊。怎么是-28800000这个数字呢
OO 
2009年1月15日 15:56
  • Comment添加评论
  • 关注(0)

2个答案按时间排序按投票排序

00

采纳的答案

calendar.clear(); 
calendar.set(1970, 0, 1); 

TimeZone tz = TimeZone.getTimeZone("GMT");        
calendar1.setTimeZone(tz); 
System.out.println(calendar.getTime()); 
System.out.println(calendar.getTimeInMillis()); 

默认时区不对.

0 0