public class Test {
private static Calendar cab;
public static void main(String[] args) {
// 如何获取两个日期的差值
Calendar c = Calendar.getInstance();
// 如何获取指定天数后的日期
c.add(Calendar.DATE, 25);
System.out.println("新的日期:" + c.getTime());
Date d=new Date();
System.out.println(d);
// 如何获取xxx分钟后的日期
c.add(Calendar.MINUTE, 61);
System.out.println(c.getTime());
Calendar cale = Calendar.getInstance();
int year = cale.get(Calendar.YEAR);
int month = cale.get(Calendar.MONTH) + 1;
int day = cale.get(Calendar.DATE);
int hour = cale.get(Calendar.HOUR_OF_DAY);
int minute = cale.get(Calendar.MINUTE);
int second = cale.get(Calendar.SECOND);
int dow = cale.get(Calendar.DAY_OF_WEEK);
int dom = cale.get(Calendar.DAY_OF_MONTH);
int doy = cale.get(Calendar.DAY_OF_YEAR);
System.out.println("Current Date: " + cale.getTime());
System.out.println("Year: " + year);
System.out.println("Month: " + month);
System.out.println("Day: " + day);
System.out.println("Hour: " + hour);
System.out.println("Minute: " + minute);
System.out.println("Second: " + second);
System.out.println("Day of Week: " + dow);
System.out.println("Day of Month: " + dom);
System.out.println("Day of Year: " + doy);
// 获取当月第一天和最后一天
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String firstday, lastday;
// 获取前月的第一天
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
firstday = format.format(cale.getTime());
// 获取前月的最后一天
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = format.format(cale.getTime());
System.out.println("本月第一天和最后一天分别是 : " + firstday + " and " + lastday);
// 获取本月最后一天的日期
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, 0);
String last=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
System.out.println("本月最后一天为"+last);
// 判断日期是否在规定的两个日期之间
Date date1 = null;
Date date2 = null;
Date date3=new Date();
try {
date1=format.parse("2017-11-12 10:12:13");
date2=format.parse("2018-11-12 10:12:13");
} catch (ParseException e) {
e.printStackTrace();
}
String s= date3.before(date1)&&date3.after(date2)?"之间":"不在";
System.out.println(s);
// 获取下次周日的日期
Calendar ca = Calendar.getInstance();
// 第一种方法 ca.set(Calendar.DAY_OF_WEEK, 1);
// System.out.println("------------");
// System.out.println(ca.getTime());
// ca.add(Calendar.DAY_OF_WEEK, 14);
// System.out.println(ca.getTime());
System.out.println("------------");
System.out.println(format.format(ca.getTime()));
System.out.println(ca.get(Calendar.DAY_OF_WEEK));
ca.add(Calendar.DATE,+7);
System.out.println("aaa"+format.format(ca.getTime()));
System.out.println(ca.get(Calendar.DAY_OF_WEEK));
System.out.println("----------------------------------");
ca.add(Calendar.DATE,+(7-Calendar.DATE));
System.out.println(format.format(ca.getTime()));
System.out.println(Calendar.DATE);
// 获取当前时间的华盛顿的日期和时间
// 将东京时间转为当前北京时间
Date dt=new Date();
Date dt1;
Date dt2;
// 设置东京时区
SimpleDateFormat tokyoSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 东京
SimpleDateFormat Washington = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 东京
tokyoSdf.setTimeZone(TimeZone.getTimeZone("GMT+9")); // 设置东京时区
Washington.setTimeZone(TimeZone.getTimeZone("GMT-4"));
SimpleDateFormat cn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cn.setTimeZone(TimeZone.getTimeZone("GMT+8"));
System.out.println(" 东京时间:" + tokyoSdf.format(dt));
System.out.println( "华盛顿时间:" + Washington.format(dt));
tokyoSdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
try {
dt1=cn.parse(tokyoSdf.format(dt));
System.out.println("东京转北京时间为"+dt1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获取某月的最后一天
Calendar abc = Calendar.getInstance();
abc.set(Calendar.MONTH, 0);
// System.out.println(abc.getTime());
abc.add(Calendar.MONTH, 12);
abc.set(Calendar.DAY_OF_MONTH, 0);
//int mon = cale.get(Calendar.MONTH) + 1;
System.out.println("某月的最后一天"+abc.getTime());
// c.set(2010, 0, 2, 14, 25, 30); // 设置时间
System.out.println("年:" + c.get(Calendar.YEAR));
System.out.println("月:" + (c.get(Calendar.MONTH) + 1)); // 月从0开始
System.out.println("日:" + c.get(Calendar.DAY_OF_MONTH)); // 可以使用Calendar.DATE
System.out.println("时(12小时制):" + c.get(Calendar.HOUR)); // 0----11
System.out.println("时(24小时制):" + c.get(Calendar.HOUR_OF_DAY)); // 0----23
System.out.println("分:" + c.get(Calendar.MINUTE));
System.out.println("秒:" + c.get(Calendar.SECOND));
Date date = c.getTime();
System.out.println("时间:" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date));
String week = null;
switch (c.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY:
week = "星期日";
break;
case Calendar.MONDAY:
week = "星期一";
break;
case Calendar.TUESDAY:
week = "星期二";
break;
case Calendar.WEDNESDAY:
week = "星期三";
break;
case Calendar.THURSDAY:
week = "星期四";
break;
case Calendar.FRIDAY:
week = "星期五";
break;
case Calendar.SATURDAY:
week = "星期六";
break;
}
System.out.println(week);
// 加5年
c.add(Calendar.YEAR, 5);
System.out.println("新的年:" + c.get(Calendar.YEAR));
// 减9个月
c.add(Calendar.MONTH, -9);
System.out.println("新的月:" + (c.get(Calendar.MONTH) + 1));
// 加25天
c.add(Calendar.DATE, 25);
System.out.println("新的日:" + c.get(Calendar.DATE));
date = c.getTime();
System.out.println("新的時間:" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date));
}
}