Java-获取当天凌晨的时间戳

获取当天凌晨的时间戳,学习了四种方式

public static void main(String[] args) {
	try {
		smallHours();
	}catch (ParseException e){
		e.printStackTrace();
	}
}

public static void smallHours() throws ParseException{
	//当天日期
	Date date = new Date();
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	String today = simpleDateFormat.format(date.getTime());
	System.out.println("当天日期" + today);

	//1、当天凌晨(毫秒)
	long daytime1 = simpleDateFormat.parse(today).getTime();
	System.out.println("1、当天凌晨(毫秒)" + daytime1);

	//2、当天凌晨(毫秒)
	long current = System.currentTimeMillis();
	long daytime2 = current-(current+ TimeZone.getDefault().getRawOffset())%(1000*3600*24);
	System.out.println("2、当天凌晨(毫秒)" + daytime2);

	//3、当天凌晨(毫秒)
	Calendar c = Calendar.getInstance();
	c.set(Calendar.HOUR_OF_DAY, 0);
	c.set(Calendar.MINUTE, 0);
	c.set(Calendar.SECOND, 0);
	c.set(Calendar.MILLISECOND, 0);
	long daytime3 = c.getTimeInMillis();
	System.out.println("3、当天凌晨(毫秒)" + daytime3);

	//4、当天凌晨(毫秒)
	long now = System.currentTimeMillis() / 1000l;
	long daySecond = 60 * 60 * 24;
	long dayTime4 = (now - (now + 8 * 3600) % daySecond) * 1000 ;
	System.out.println("4、当天凌晨(毫秒)" + dayTime4);
}

运行结果:

当天日期2019-08-22
1、当天凌晨(毫秒)1566403200000
2、当天凌晨(毫秒)1566403200000
3、当天凌晨(毫秒)1566403200000
4、当天凌晨(毫秒)1566403200000

你可能感兴趣的:(Java)