package com.yonge.toolset.utils.date;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

public class DateConvertor {

	public static LocalDateTime toLocalDateTime(Date date) {
		Instant instant = date.toInstant();
		ZoneId zone = ZoneId.systemDefault();
		return LocalDateTime.ofInstant(instant, zone);
	}

	public static LocalDate toLocalDate(Date date) {
		Instant instant = date.toInstant();
		ZoneId zone = ZoneId.systemDefault();
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
		return localDateTime.toLocalDate();
	}

	public static LocalTime toLocalTime(Date date) {
		Instant instant = date.toInstant();
		ZoneId zone = ZoneId.systemDefault();
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
		return localDateTime.toLocalTime();
	}

	public static Date toDate(LocalDateTime localDateTime) {
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return Date.from(instant);
	}

	public static Date toDate(LocalDate localDate) {
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
		return Date.from(instant);
	}

	public static Date toDate(LocalTime localTime) {
		LocalDate localDate = LocalDate.now();
		LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return Date.from(instant);
	}
	
	public static void main(String[] args) {
		Date date = new Date();
		LocalDate localDate = DateConvertor.toLocalDate(date);
		System.out.println("Date to LocalDate:"+localDate);
		
		System.out.println("LocalDate to Date:"+DateConvertor.toDate(localDate));
		
		LocalDateTime localDateTime = DateConvertor.toLocalDateTime(date);
		System.out.println("Date to LocalDateTime:"+ localDateTime);
		
		System.out.println("LocalDateTime to Date"+DateConvertor.toDate(localDateTime));
		
		LocalTime localTime = DateConvertor.toLocalTime(date);
		System.out.println("Date to LocalTime:"+localTime);
		
		System.out.println("localTime to Date:"+ DateConvertor.toDate(localTime));
	}
}
