- java8 LocaleDateTime 获取
public static void main(String[] args) {
LocalDateTime date = LocalDateTime.now();
// 获取一天的开始时间
LocalDateTime startOfTheDay = LocalDateTime.of(date.toLocalDate(), LocalTime.MIN);
System.out.println("startOfTheDay = " + startOfTheDay);
// 获取一天的结束时间
LocalDateTime endOfTheDay = LocalDateTime.of(date.toLocalDate(), LocalTime.MAX);
System.out.println("endOfTheDay = " + endOfTheDay);
}
- Calendar 获取
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* Calendar.HOUR_OF_DAY 是24小时
* Calendar.HOUR 是12小时
*/
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
String startTime = sdf.format(todayStart.getTime());
System.out.println("startTime = " + startTime);
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
System.out.println("todayStart = " + todayStart.getTime());
System.out.println("todayEnd = " + todayEnd.getTime());
文章来源地址https://www.toymoban.com/news/detail-622029.html
文章来源:https://www.toymoban.com/news/detail-622029.html
到了这里,关于java获取一天的开始时间和结束时间的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!