java时间运算

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");// a为am/pm的标记  
        Date date = new Date();// 获取当前时间 
        System.out.println("现在时间:" + sdf.format(date)); // 输出已经格式化的现在时间(24小时制) 
        }
}

2.获取某一年中某一周的周几的时间

//计算某一年中的第多少周的周一和周日的日期时间
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2019); // 2019年
cal.set(Calendar.WEEK_OF_YEAR, 53); // 设置为2019年的第33周
cal.set(Calendar.DAY_OF_WEEK, 2); // 1表示周日,2表示周一,7表示周六
Date date = cal.getTime();

cal.set(Calendar.YEAR, 2019); // 2019年
cal.set(Calendar.WEEK_OF_YEAR, 54); // 设置为2019年的第34周
cal.set(Calendar.DAY_OF_WEEK, 1); // 1表示周日,2表示周一,7表示周六
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");

//得到相差的天数 betweenDate
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. 在某个时间的基础上获取其前后一个固定天数的日期。

//某个日期之前7天
int past = -7;
//某个日期之后7天
//int past = 7;

//创建SimpleDateFormat并设置时间格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dateTime = null;
try {
    //将以上格式的的字符串endTime转换成时间
    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. 查询某特定时间之前或之后的特定几个月的时间日期

//查询当前时间之后6个月时间
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); // 星期日为一周的第一天输出为 1,星期一输出为 2,以此类推
System.out.println("一月中的第几天: " + dom);
System.out.println("一年的第几天: " + doy)

9. 获取当前时间到当天凌晨12点毫秒数

//获取当前时间到今天晚上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);

你可能感兴趣的:(java时间运算)