Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
package gov.va.med.pbm.ampl.utility;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import gov.va.med.pbm.ampl.constant.AmplConstants;
import gov.va.med.pbm.ampl.constant.MockDataConstants;
/**
* A common core of Date operations.
*
* @author Ian Meinert, Asli Goncer
* @since 1.0
*
*/
public class DateUtility {
/**
* The application logger.
*/
static final Logger LOGGER = LoggerFactory.getLogger(DateUtility.class);
/**
* Default constructor for the static methods.
*/
private DateUtility() {
// Not used.
}
/**
* Convert a String to Date. If the String does not match any of the below patterns, a DateTimeParseException will be
* thrown.
*
* @param originalString the String to be converted
* @return a Date
* @throws DateTimeParseException dateTimeParseException
*/
public static Date dateValueOf(String originalString) throws DateTimeParseException {
LocalDate localDate = LocalDate.parse(originalString.split(" ")[0], dateTimeFormatter());
ZonedDateTime zdt = localDate.atStartOfDay(ZoneId.systemDefault());
Instant instant = zdt.toInstant();
return Date.from(instant);
}
/**
* Converts a LocalDateTime object to Date.
*
* @param localDateTime the LocalDateTime object
* @return a Date
*/
public static Date dateValueOf(LocalDateTime localDateTime) {
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
return date;
}
/**
* Gets the time stamp of a Date.
*
* @param date The Date object to be parsed
* @return TimeStamp
*/
public static Timestamp timeStampValueOf(Date date) {
return new Timestamp(date.getTime());
}
/**
* Gets the time stamp of a String date.
*
* @param date The String object to be parsed
* @return TimeStamp
*/
public static Timestamp timeStampValueOf(String date) {
return new Timestamp(DateUtility.dateValueOf(date).getTime());
}
/**
* Creates a random date in String format.
*
* @param start the earliest year allowable
* @param end the latest year allowable
* @return String
*/
public static String randomDate(int start, int end) {
GregorianCalendar gc = new GregorianCalendar();
int year = NumberUtility.randomBetween(start, end);
int dayOfYear = gc.getActualMaximum(Calendar.DAY_OF_YEAR);
int day = NumberUtility.randomBetween(1, dayOfYear);
gc.set(Calendar.YEAR, year);
gc.set(Calendar.DAY_OF_YEAR, day);
return String.format("%02d", (gc.get(Calendar.MONTH) + 1)) + "/" + String.format("%02d", gc.get(Calendar.DAY_OF_MONTH))
+ "/" + String.format("%04d", gc.get(Calendar.YEAR));
}
/**
* Creates a random {@link OffsetDateTime} given a range of years. The time is in HH:MM:SS format.
*
* @param startYear the earliest year allowable
* @param endYear the latest year allowable
* @return generated {@link OffsetDateTime}
*/
public static OffsetDateTime randomOffsetDateTime(int startYear, int endYear) {
LocalDate date = dateValueOf(randomDate(startYear, endYear)).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalTime time = LocalTime.of(NumberUtility.randomBetween(0, AmplConstants.RANDOM_TIME_HOUR_MAX),
NumberUtility.randomBetween(0, AmplConstants.RANDOM_TIME_MIN_MAX),
NumberUtility.randomBetween(0, AmplConstants.RANDOM_TIME_SEC_MAX));
OffsetDateTime dateTime = LocalDateTime.of(date, time).atOffset(ZoneOffset.UTC);
return dateTime;
}
/**
* Convert a Date to LocalDateTime.
*
* @param originalDate the Date to convert
* @return LocalDateTime value of Date
*/
public static LocalDateTime localDateTimeValueOf(Date originalDate) {
return originalDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/**
* Convert a Date to LocalDateTime.
*
* @param originalString the String to convert
* @return LocalDateTime value of Date
*/
public static LocalDateTime localDateTimeValueOf(String originalString) {
return LocalDateTime.parse(originalString, dateTimeFormatter());
}
/**
* Convert a Date to OffsetDateTime.
*
* @param originalDate the Date to convert
* @return OffsetDateTime value of Date
*/
public static OffsetDateTime offsetDateTimeValueOf(Date originalDate) {
return originalDate.toInstant().atOffset(ZoneOffset.UTC);
}
/**
* Convert a String to {@linl OffsetDateTime}.
*
* @param originalDateString the date to convert
* @return OffsetDateTime value of Date
*/
public static OffsetDateTime offsetDateTimeValueOf(String originalDateString) {
if (isValidDateTimeFormat(originalDateString)) {
return OffsetDateTime.parse(originalDateString, dateTimeFormatter());
} else {
return OffsetDateTime.of(localDateTimeValueOf(dateValueOf(originalDateString)), ZoneOffset.UTC);
}
}
/**
* Performs a check to determine if a given String is a valid DateTime.
* <p>
* If, after parsing the string, the parser does not catch a DateTimeParseException, then the string is a valid date format
*
* @param originalString the String to check
* @return boolean value determined if the param is a valid date format
*/
public static boolean isValidDateTimeFormat(String originalString) {
try {
LocalDateTime.parse(originalString, dateTimeFormatter().withZone(ZoneOffset.UTC));
} catch (DateTimeParseException e) {
return false;
}
return true;
}
/**
* Performs a check to determine if a given String is a valid date.
* <p>
* If, after parsing the string, the parser does not catch a DateTimeParseException, then the string is a valid date format
*
* @param originalString the String to check
* @return boolean value determined if the param is a valid date format
*/
public static boolean isValidDateFormat(String originalString) {
try {
LocalDate.parse(originalString, dateTimeFormatter());
} catch (DateTimeParseException e) {
return false;
}
return true;
}
/**
* This method sets the standard for DateTime formats.
*
* @return {@link DateTimeFormatter}
*/
public static DateTimeFormatter dateTimeFormatter() {
// return DateTimeFormatter.ISO_OFFSET_DATE_TIME;
return new DateTimeFormatterBuilder()
// full ISO8601 with date/time and UTC offset (ex: 2011-12-03T10:15:30+01:00)
.appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
// some custom (day/month/year) formats
.appendPattern("[uuuu-MM-dd]").appendPattern("[M/dd/uuuu]").appendPattern("[M/d/uuuu]")
.appendPattern("[MM/dd/uuuu]").appendPattern("[MMdduuuu]").appendPattern("[MMM dd uuuu]")
.appendPattern("[uuuu-MM-dd HH:mm:ss]").appendPattern("[M/dd/uuuu HH:mm:ss]").appendPattern("[M/d/uuuu HH:mm:ss]")
.appendPattern("[MM/dd/uuuu HH:mm:ss]").appendPattern("[MMdduuuu HH:mm:ss]").appendPattern("[MMM dd uuuu HH:mm:ss]")
.toFormatter(Locale.ENGLISH);
}
}