主要介绍了Calendar类的使用
输出
* 时间格式化
* 当前时间:2016-12-02 16:46:27.079
*
* 转换:String-->Date-->Calendar
* 设置时间(方式1):2014-08-01 09:18:34.123
* 设置时间(方式2):2016-12-01 19:08:54.123
*
* ----YEAR-MONTH----
* YEAR:2016
* MONTH:11
* ----Day-----
* 本月第几天:1
* 本周几:5
* 今年的第 N天 :336
* DAY_OF_WEEK_IN_MONTH:1
* ----Hour-----
* Hour:7
* HOUR_OF_DAY:19
* ----MINUTE-----
* MINUTE:8
*
* 向后推迟一个月(方式1):2017-01-01 19:08:54.123
* 向后推迟一个月(方式2):2017-02-01 19:08:54.123
* 向后推迟一小时(方式1):2017-02-01 20:08:54.123
* 向前推迟一小时(方式2):2017-02-01 19:08:54.123
* 设置为30分钟:2017-02-01 19:30:00.123
*
* 开始时间:2014-12-15 16:46:27.116,毫秒数:1418633187116
* 结束时间:2016-12-05 12:15:35.117,毫秒数:1480911335117
* 相差天数:721
*
* 本月最大天数:31
程序
package com.sssppp.DateTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarExample {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println("时间格式化");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println("当前时间:" + sdf.format(cal.getTime()));
System.out.println();
try {
System.out.println("转换:String-->Date-->Calendar");
cal.setTime(sdf.parse("2014-8-1 9:18:34.123"));
System.out.println("设置时间(方式1):" + sdf.format(cal.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}
//2016-12-01 19:08:54
cal.set(2016, Calendar.DECEMBER, 1, 19, 8, 54);
System.out.println("设置时间(方式2):" +sdf.format(cal.getTime()));
System.out.println();
System.out.println("----YEAR-MONTH----");
System.out.println("YEAR:" + cal.get(Calendar.YEAR));
System.out.println("MONTH:" + cal.get(Calendar.MONTH));
System.out.println("----Day-----");
System.out.println("本月第几天:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("本周几:" + cal.get(Calendar.DAY_OF_WEEK));
System.out.println("今年的第 N天 :" + cal.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK_IN_MONTH:"
+ cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("----Hour-----");
System.out.println("Hour:" + cal.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("----MINUTE-----");
System.out.println("MINUTE:" + cal.get(Calendar.MINUTE));
System.out.println();
cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+1);
System.out.println("向后推迟一个月(方式1):" + sdf.format(cal.getTime()));
cal.add(Calendar.MONDAY, 1);
System.out.println("向后推迟一个月(方式2):" + sdf.format(cal.getTime()));
cal.set(Calendar.HOUR,cal.get(Calendar.HOUR)+1);
System.out.println("向后推迟一小时(方式1):" + sdf.format(cal.getTime()));
cal.add(Calendar.HOUR, -1);
System.out.println("向前推迟一小时(方式2):" + sdf.format(cal.getTime()));
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,00);
System.out.println("设置为30分钟:" + sdf.format(cal.getTime()));
cal.getTimeInMillis();
System.out.println();
Calendar calStart = Calendar.getInstance();
calStart.set(2014, Calendar.DECEMBER, 15);
System.out.println("开始时间:" + sdf.format(calStart.getTimeInMillis())
+ ",毫秒数:" + calStart.getTimeInMillis());
Calendar calEnd = Calendar.getInstance();
calEnd.set(2016, Calendar.DECEMBER, 5, 12, 15, 35);
System.out.println("结束时间:" + sdf.format(calEnd.getTimeInMillis())
+ ",毫秒数:" + calEnd.getTimeInMillis());
int day =getDaysBetween(calEnd,calStart);
System.out.println("相差天数:" + day);
//计算某一月份的最大天数
System.out.println();
Calendar time=Calendar.getInstance();
time.clear(); // 在使用set方法之前,必须先clear一下,否则很多信息会继承自系统当前时间
time.set(Calendar.YEAR, 2016);
int dayCount = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
System.out.println("本月最大天数:" + dayCount);
}
/**
* 计算两个时间之间相隔天数(不够精确)
*
* @param startday
* 开始时间
* @param endday
* 结束时间
* @return
*/
public static int getIntervalDays(Calendar startday, Calendar endday) {
// 确保startday在endday之前
if (startday.after(endday)) {
Calendar cal = startday;
startday = endday;
endday = cal;
}
// 分别得到两个时间的毫秒数
long sl = startday.getTimeInMillis();
long el = endday.getTimeInMillis();
long ei = el - sl;
// 根据毫秒数计算间隔天数
return (int) (ei / (1000 * 60 * 60 * 24));
}
/**
* 计算两个时间之间相隔天数(不够精确)
*
* @param startday
* 开始时间
* @param endday
* 结束时间
* @return
*/
public int getIntervalDays(Date startday, Date endday) {
// 确保startday在endday之前
if (startday.after(endday)) {
Date cal = startday;
startday = endday;
endday = cal;
}
// 分别得到两个时间的毫秒数
long sl = startday.getTime();
long el = endday.getTime();
long ei = el - sl;
// 根据毫秒数计算间隔天数
return (int) (ei / (1000 * 60 * 60 * 24));
}
/**
* 精确计算两个日期之间的天数
*
* @param d1
* @param d2
* @return
*/
public static int getDaysBetween(Calendar d1, Calendar d2) {
if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
int y2 = d2.get(Calendar.YEAR);
if (d1.get(Calendar.YEAR) != y2) {
d1 = (Calendar) d1.clone();
do {
days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数
d1.add(Calendar.YEAR, 1);
} while (d1.get(Calendar.YEAR) != y2);
}
return days;
}
}
转载请标明出处:http://www.cnblogs.com/ssslinppp/