com.ch.util.DateTimeUtils Maven / Gradle / Ivy
/**
*
*/
package com.ch.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* @author ravindra
*
*/
public class DateTimeUtils {
private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static String getSystemTimeZone() {
Calendar calendar = Calendar.getInstance();
TimeZone timeZone = calendar.getTimeZone();
return timeZone.getID();
}
public static String convertDateToUTC(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
TimeZone timeZone = TimeZone.getTimeZone("UTC");
sdf.setTimeZone(timeZone);
return sdf.format(date);
}
public static String calculateEndTime(Date startDate, Date endDate) {
long duration = endDate.getTime() - startDate.getTime();
String format = String.format("%%0%dd", 2);
duration = duration / 1000;
String seconds = String.format(format, duration % 60);
String minutes = String.format(format, (duration % 3600) / 60);
String hours = String.format(format, duration / 3600);
return hours + ":" + minutes + ":" + seconds;
}
public static boolean isValid(String dateStr) {
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
sdf.setLenient(false);
try {
sdf.parse(dateStr);
} catch (ParseException e) {
return false;
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy