그냥 오늘 날짜를 가져와야하는데, 시스템이 utc도 있고 아닌 곳도 있고 해서 만들어봤다.
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class DateTimeHelper {
//https://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java#19632076
public static OffsetDateTime getDateTimeNowUtc() {
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
return now;
}
public static OffsetDateTime getDateTimeNowOffset(int hour, int min) {
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.ofHoursMinutes(hour, min));
return now;
}
public static OffsetDateTime getDateTimeNowOffset(int minute) {
int hour = minute / 60;
int min = minute % 60;
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.ofHoursMinutes(hour, min));
return now;
}
public static String getDateString(int hour, int min, DateTimeFormatter formatter) {
OffsetDateTime time = getDateTimeNowOffset(hour, min);
return time.format(formatter);
}
public static String getDateString(int hour, int min) {
OffsetDateTime time = getDateTimeNowOffset(hour, min);
return time.format(DateTimeFormatter.ISO_LOCAL_DATE);
}
public static String getDateString(int min) {
OffsetDateTime time = getDateTimeNowOffset(min);
return time.format(DateTimeFormatter.ISO_LOCAL_DATE);
}
public static OffsetDateTime getDateOffset(String date, String pattern, int offsetMin) {
int hour = offsetMin / 60;
int min = offsetMin % 60;
LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
OffsetDateTime odate = localDate.atStartOfDay(ZoneOffset.ofHoursMinutes(hour, min)).toOffsetDateTime();
return odate;
}
public static OffsetDateTime getDateTimeOffset(String datetime, String pattern, int offsetMin) {
int hour = offsetMin / 60;
int min = offsetMin % 60;
LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(pattern));
LocalDate ldate = localDateTime.toLocalDate();
OffsetDateTime odate = ldate.atStartOfDay(ZoneOffset.ofHoursMinutes(hour, min)).toOffsetDateTime();
OffsetDateTime result = odate.plusHours(localDateTime.getHour()).plusMinutes(localDateTime.getMinute()).plusSeconds(localDateTime.getSecond());
return result;
}
public static Timestamp getUTCTimestamp(OffsetDateTime date) {
Timestamp stamp = Timestamp.valueOf(date.atZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
return stamp;
}
}