1 . 获取当前时间并格式化
public class Demo {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
Date date = new Date();
System.out.println("现在时间:" + sdf.format(date));
}
}
2.获取某一年中某一周的周几的时间
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2019);
cal.set(Calendar.WEEK_OF_YEAR, 53);
cal.set(Calendar.DAY_OF_WEEK, 2);
Date date = cal.getTime();
cal.set(Calendar.YEAR, 2019);
cal.set(Calendar.WEEK_OF_YEAR, 54);
cal.set(Calendar.DAY_OF_WEEK, 1);
Date date01 = cal.getTime();
System.out.println("周一:===:"+df.format(date));
System.out.println("周日:===:"+df.format(date01));
3. 比较量两个时间的大小
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date dt1 =df.parse("2019-02-03");
Date dt2 =new Date();
if (dt1.getTime() > dt2.getTime()) {
System.out.println(df.format(dt1.getTime())+"比较大");
} else if(dt1.getTime() < dt2.getTime()) {
System.out.println(df.format(dt2.getTime())+"是当前时间,当前时间比较大");
}
if(dt1.getTime() == dt2.getTime()){
System.out.println("两个时间一样");
}
4. 获取两个日期之间的相差的天数
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = sdf.parse("2019-08-05");
Date endDate = sdf.parse("2019-08-10");
long betweenDate = (endDate.getTime() - startDate.getTime())/(60*60*24*1000);
System.out.println(sdf.format(startDate)+"与 "+sdf.format(endDate)+"相差"+betweenDate+"天");
5. 获取两个日期之间相差的月数
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str1 = "2020-04-27 12:15:03";
String str2 = "2018-04-27 01:34:21";
Calendar bef = Calendar.getInstance();
Calendar aft = Calendar.getInstance();
bef.setTime(sdf.parse(str1));
aft.setTime(sdf.parse(str2));
int surplus = aft.get(Calendar.DATE) - bef.get(Calendar.DATE);
int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
System.out.println(surplus);
surplus = surplus <= 0 ? 1 : 0;
System.out.println(surplus);
System.out.println("相差月份:" + (Math.abs(month + result) + surplus-1));
}
6. 在某个时间的基础上获取其前后一个固定天数的日期。
int past = -7;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dateTime = null;
try {
dateTime = simpleDateFormat.parse("2019-03-05");
} catch (Exception e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String result = format.format(today);
System.out.println("result==="+result);
7. 查询某特定时间之前或之后的特定几个月的时间日期
int renewalsdata = 6;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
System.out.println(sdf.format(calendar.getTime()));
calendar.add(Calendar.MONTH, renewalsdata);
System.out.println(sdf.format(calendar.getTime()));
8. 日期与周的转换
Date time = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(time);
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
int dow = cal.get(Calendar.DAY_OF_WEEK);
int dom = cal.get(Calendar.DAY_OF_MONTH);
int doy = cal.get(Calendar.DAY_OF_YEAR);
String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
cal.setTime(time);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
System.out.println("weekDays[w]===="+weekDays[w]);
System.out.println("当期时间: " + cal.getTime());
System.out.println("日期: " + day);
System.out.println("月份: " + month);
System.out.println("年份: " + year);
System.out.println("一周的第几天: " + dow);
System.out.println("一月中的第几天: " + dom);
System.out.println("一年的第几天: " + doy)
9. 获取当前时间到当天凌晨12点毫秒数
long current = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long tomorrowzero = calendar.getTimeInMillis();
long tomorrowzeroSeconds = (tomorrowzero - current);
System.out.println("当前时间毫秒数:"+current);
System.out.println("不知道是啥"+tomorrowzero);
System.out.println("现在到今晚12点的毫秒数:"+tomorrowzeroSeconds);