package 时间;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class mycalendar {
private static int compare_date(String string, String string2) {
// 比较日期大小
SimpleDateFormat ss = new SimpleDateFormat("y-MM-dd hh:mm");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
try {
Date dt1 = df.parse(string);
Date dt2 = df.parse(string2);
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else if (dt1.getTime() == dt2.getTime()) {
return 0;
}
return 999;
} catch (ParseException e) {
return 999;
}
}
static int days(Date x, Date y) {
long n = x.getTime() - y.getTime();
return (int) (n / (24 * 60 * 60 * 1000));
}
static int dayss(Calendar x, Calendar c) {
return x.get(x.DAY_OF_YEAR) - c.get(c.DAY_OF_YEAR);
}
public static String getWeek() {
String str[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };// 字符串数组
return str[Calendar.getInstance().get(Calendar.getInstance().DAY_OF_WEEK) - 1];
}
@SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException, ParseException {
Calendar x = Calendar.getInstance();
Calendar c = new GregorianCalendar();
SimpleDateFormat si = new SimpleDateFormat("y-MM-dd hh:mm:ss", Locale.CHINESE);
c.set(2017, 8, 25, 1, 20, 21);
x.setLenient(true);// 容错性设置
System.out.println("两日期相差:" + dayss(x, c));
System.out.println(c.getTime());
System.out.println("当前系统时间:" + x.getTime());
System.out.print(x.get(Calendar.YEAR) + "年:");
System.out.print(x.get(Calendar.MONTH) + "月:");
System.out.print(x.get(Calendar.DAY_OF_WEEK) + "日:");
System.out.print(getWeek() + ":");
System.out.print(x.get(Calendar.HOUR_OF_DAY) + "时:");
System.out.print(x.get(Calendar.MINUTE) + "分:");
System.out.println(x.get(Calendar.SECOND) + "秒");
System.out.println("本年度的第几周" + x.get(Calendar.WEEK_OF_YEAR));
System.out.println("本月第几周:" + x.get(Calendar.WEEK_OF_MONTH));
System.out.println("本周第几天:" + x.get(Calendar.DAY_OF_WEEK));
System.out.println(x.getClass().getName());
System.out.println(x.getTimeInMillis());
x.set(2199, 9, 9);// 设置年月日
System.out.println(x.getTime());
x.set(Calendar.YEAR, 2000);// 设置年
x.set(Calendar.HOUR_OF_DAY, 15);// 设置时
System.out.println(x.getTime());
x.roll(Calendar.YEAR, 2);// 年数加2
x.roll(Calendar.HOUR_OF_DAY, false);// false:-1 true:+1
System.out.println(x.getTime());
x.set(2000, 9, 9);
x.add(Calendar.YEAR, 2);// 年数加2
System.out.println("当前时间:" + x.getTime());
x.set(2017, 8, 15, 12, 6, 60);// 自动进位
System.out.println(x.getTime());
System.out.println("一年中最大月份:" + x.getActualMaximum(Calendar.MONTH));
System.out.println("一年中最小 月份:" + x.getActualMinimum(Calendar.MONTH));
System.out.println("一年天数" + x.getActualMaximum(Calendar.DAY_OF_YEAR));
System.out.println("一月最大天数" + x.getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println(x.getActualMinimum(Calendar.DAY_OF_MONTH));
System.out.println(si.format(x.getTime()));// 格式化时间
c.setTime(new SimpleDateFormat("yy-mm-dd").parse("2017-10-28"));// 字符串转时间
System.out.println(si.format(c.getTime()));
c.set(Calendar.DATE, Calendar.DATE + 1);// 天数减一
System.out.println("后一天:" + si.format(c.getTime()));
System.out.println(System.nanoTime() / 1000 / 1000 / 1000 / 60 / 60 / 24 / 365);
System.out.println("当前时间距离1970年年数:" + System.currentTimeMillis() / 1000 / 60 / 60 / 24 / 365);
c.setTime(Calendar.getInstance().getTime());
System.out.println("当前时间:" + si.format(c.getTime()));
c.add(Calendar.MONTH, -2);
System.out.println("当前时间:" + si.format(c.getTime()));
// *****计算某年某月多少天********
int nowYear = 1995;
c.set(Calendar.YEAR, nowYear);
c.set(Calendar.DATE, 1);
for (int i = 0; i < 11; i++) {
c.set(Calendar.MONTH, i);
System.out.println(nowYear + "年" + (i + 1) + "月" + c.getActualMaximum(Calendar.DATE));
}
int i = compare_date("2017-07-21 15:21", "2017-07-21 15:22:99");
System.out.println("i==" + i);
System.out.println("两日期相差天数" + days(new Date(2017, 7, 21), new Date(2017, 8, 21)));
}
}
JAVA时间类
最新推荐文章于 2024-06-20 14:22:35 发布