ranyeli
10/8/2017 - 3:06 AM

DTO javascript Date to java LocalDateTime

This DTO turn the javascript string representation of a date into a LocalDateTime

package com.project.dto;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class UniversalTimeDTO {
	
	private String date1;
	
	private String date2;
	
	private DateTimeFormatter format;
		
	public LocalDateTime getDate1() {
//		2015-12-10T00:00:00.000Z
		LocalDateTime date1 = LocalDateTime.parse(this.date1, format);
		return date1;
	}

	public void setDate1(String date1) {
		this.date1 = date1;
	}

	public LocalDateTime getDate2() {
		if(this.date2 == null) this.date2 = "1970-01-01T00:00:00.000Z";
		LocalDateTime date2 = LocalDateTime.parse(this.date2, format);
		return date2;
	}

	public void setDate2(String date2) {
		this.date2 = date2;
	}

	public LocalDateTime getDate3() {
		return LocalDateTime.now(ZoneId.of("UTC"));
	}

	public UniversalTimeDTO() {
		this.format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
		// TODO Auto-generated constructor stub
	}
}