Java Calendar 类时间操作,这也许是创建和管理日历最简单的一个方案,示范代码很简单。
演示了获取时间,日期时间的累加和累减,以及比较。
原文地址:blog.csdn.NET/joyous/article/details/9630893
注意事项:
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 的格式定义
java Calendar 演示代码如下:
- package demo;
-
- import java.util.Date;
- import java.text.SimpleDateFormat;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.util.Calendar;
-
- public class Test
- {
- public Test()
- {
- }
-
- public static void main(String[] args)
- {
-
-
-
-
-
-
-
-
- String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());
- System.out.println(str);
-
-
- Calendar calendar = Calendar.getInstance();
-
- try
- {
-
-
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");
-
- Date date = dateFormat.parse("2013-6-1 13:24:16");
-
- calendar.setTime(date);
-
-
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
- System.out.println(str);
- }
- catch (ParseException e)
- {
- e.printStackTrace();
- }
-
-
-
- calendar = Calendar.getInstance();
- calendar.set(2013, 1, 2, 17, 35, 44);
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
- System.out.println(str);
-
-
-
- calendar = Calendar.getInstance();
-
- calendar.setTime(new Date());
-
-
-
-
-
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
- System.out.println(str);
-
-
- int year = calendar.get(Calendar.YEAR);
- System.out.println("year is = " + String.valueOf(year));
-
-
- int month = calendar.get(Calendar.MONTH);
- System.out.println("nth is = " + (month + 1));
-
-
- int week = calendar.get(Calendar.DAY_OF_WEEK);
- System.out.println("week is = " + week);
-
-
- int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
- System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);
-
-
- int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
- System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));
-
-
- calendar.add(Calendar.HOUR_OF_DAY, 3);
- int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
- System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);
-
-
- int MINUTE = calendar.get(Calendar.MINUTE);
- System.out.println("MINUTE = " + MINUTE);
-
-
- calendar.add(Calendar.MINUTE, 15);
- MINUTE = calendar.get(Calendar.MINUTE);
- System.out.println("MINUTE + 15 = " + MINUTE);
-
-
- calendar.add(Calendar.MINUTE, -30);
- MINUTE = calendar.get(Calendar.MINUTE);
- System.out.println("MINUTE - 30 = " + MINUTE);
-
-
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
- System.out.println(str);
-
-
- calendar.setTime(new Date());
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
- System.out.println(str);
-
-
- Calendar calendarNew = Calendar.getInstance();
-
-
- calendarNew.add(Calendar.HOUR, -5);
- System.out.println("时间比较:" + calendarNew.compareTo(calendar));
-
-
- calendarNew.add(Calendar.HOUR, +7);
- System.out.println("时间比较:" + calendarNew.compareTo(calendar));
-
-
- calendarNew.add(Calendar.HOUR, -2);
- System.out.println("时间比较:" + calendarNew.compareTo(calendar));
- }
- }
要计算时间差,可用 Calendar.getTimeInMillis() 取得两个时间的微秒级的时间差,再加以换算即可,比如获得相差天数,代码如下:
-
- long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis();
-
- long day = val / (1000 * 60 * 60 * 24);