栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java编程时间日期API实例解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java编程时间日期API实例解析

本文实例主要是关于Java8中的新特性,时间日期api的相关实例,具体如下:

package com.effective.common.base.date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class LocalDateUtils {
	private static ZoneId zone = ZoneId.systemDefault();
	
	public static Date convertToDate(String date) throws Exception{
		LocalDate localDate = null;
		if(null == date){
			throw new NullPointerException("date isn't null");
		} else {
			localDate = LocalDate.parse(date);
			return convertToDate(localDate);
		}
	}
	
	public static LocalDateTime convertToLocalDateTime(String date){
		LocalDateTime localDateTime = null;
		if(null == date){
			throw new NullPointerException("date isn't null");
		} else {
			localDateTime = LocalDateTime.parse(date);
			return localDateTime;
		}
	}
	
	public static Date convertToDate(LocalDate localDate){
		Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
		return Date.from(instant);
	}
	
	public static Date convertToDate(LocalDateTime localDateTime){
		Instant instant = localDateTime.atZone(zone).toInstant();
		return Date.from(instant);
	}
	
	public static LocalDate convertToLocalDate(Date date){
		Instant instant = date.toInstant();
		return convertToLocalDateTime(instant).toLocalDate();
	}
	
	public static LocalTime convertToLocalTime(Date date){
		Instant instant = date.toInstant();
		return convertToLocalDateTime(instant).toLocalTime();
	}
	
	public static LocalDateTime convertToLocalDateTime(Date date){
		Instant instant = date.toInstant();
		return convertToLocalDateTime(instant);
	}
	
	public static LocalDateTime convertToLocalDateTime(Instant instant){
		return LocalDateTime.ofInstant(instant, zone);
	}
	
	public static Instant convertToInstant(LocalDateTime localDateTime){
		return localDateTime.atZone(zone).toInstant();
	}
	
	public static Instant convertToInstant(LocalDate localDate){
		return localDate.atStartOfDay(zone).toInstant();
	}
	
	public static LocalDateTime convertToLocalDateTime(LocalDate localDate){
		return localDate.atStartOfDay();
	}
	
	public static String formatter(LocalDateTime localDateTime, String formatStyle){
		return DateTimeFormatter.ofPattern(formatStyle).format(localDateTime);
	}
	
	public static LocalDateTime setYear(LocalDateTime sourceDate, Integer year){
		return sourceDate.withYear(year);
	}
	
	public static LocalDateTime setMonth(LocalDateTime sourceDate, Integer month){
		return sourceDate.withMonth(month);
	}
	
	public static LocalDateTime setDayOfMonth(LocalDateTime sourceDate, Integer dayOfMonth){
		return sourceDate.withDayOfMonth(dayOfMonth);
	}
	
	public static LocalDateTime setHour(LocalDateTime sourceDate,Integer hour){
		return sourceDate.withHour(hour);
	}
	
	public static LocalDateTime setMinute(LocalDateTime sourceDate,Integer minute){
		return sourceDate.withMinute(minute);
	}
	
	public static LocalDateTime setSecond(LocalDateTime sourceDate,Integer second){
		return sourceDate.withSecond(second);
	}
	
	public static LocalDateTime setYMD(LocalDateTime sourceDate, Integer year, Integer month, Integer dayOfMonth) {
		return sourceDate.withYear(year).withMonth(month).withDayOfMonth(dayOfMonth);
	}
	
	public static LocalDateTime setHMS(LocalDateTime sourceDate,Integer hour, Integer minute, Integer second) {
		return sourceDate.withHour(hour).withMinute(minute).withSecond(second);
	}
	
	public static int getInteverDays(LocalDate beginDate,LocalDate endDate){
		Period period = Period.between(beginDate, endDate);
		return period.getDays();
	}
	
	@SuppressWarnings("static-access")
	  public static LocalDate addLocalDate(long num,ChronoUnit unit,final LocalDate localDate){
		LocalDate resultDate;
		if(num > 0){
			resultDate = localDate.now().plus(num, unit);
		} else {
			resultDate = localDate.now().minus(Math.abs(num), unit);
		}
		return resultDate;
	}
	
	@SuppressWarnings("static-access")
	  public static LocalDateTime addLocalDateTime(long num,ChronoUnit unit,LocalDateTime localDateTime){
		LocalDateTime resultDateTime;
		if(num > 0){
			resultDateTime = localDateTime.now().plus(num, unit);
		} else {
			resultDateTime = localDateTime.now().minus(Math.abs(num),unit);
		}
		return resultDateTime;
	}
	
	@SuppressWarnings("static-access")
	  public static LocalTime addLocalTime(long num,ChronoUnit unit,LocalTime localTime){
		LocalTime resultTime;
		if(num > 0){
			resultTime = localTime.now().plus(num, unit);
		} else {
			resultTime = localTime.now().minus(Math.abs(num), unit);
		}
		return resultTime;
	}
	public static void main(String[] args){
		LocalDateTime time = LocalDateTime.now();
		String rr = formatter(time, "yyyy-MM-dd HH:mm:ss");
		System.out.println(rr);
		LocalDateTime time2 = addLocalDateTime(-2, ChronoUnit.HOURS, time);
		String yy = formatter(time2, "yyyy-MM-dd HH:mm:ss");
		System.out.println(yy);
	}

代码涉及时间日期类的使用等内容,具有简单注释,大家可自行参考。

总结

以上就是本文关于Java编程时间日期API实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/142191.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号