Skip to content

Instantly share code, notes, and snippets.

@steklopod
Created March 21, 2019 09:10
Show Gist options
  • Save steklopod/d3adbfa43f93423678eae5893ea2e90e to your computer and use it in GitHub Desktop.
Save steklopod/d3adbfa43f93423678eae5893ea2e90e to your computer and use it in GitHub Desktop.
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Optional;
import java.util.TimeZone;
import static java.util.TimeZone.getTimeZone;
public class DateUtil {
public static final String LOCALE_RU = "ru";
public static final String PATTERN_ddMMyyyy = "dd.MM.yyyy";
public static final String PATTERN_ddMMMMyyyy = "dd MMMMM yyyy г.";
public static final String PATTERN_HHmmss = "HH:mm:ss";
public static final DateFormat DF_ddMMyyyy = new SimpleDateFormat(PATTERN_ddMMyyyy);
public static final DateFormat DF_yyyyMMddTHHmmssXXX = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
public static final DateFormat DF_yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
public static final DateFormat DF_ddMMyyyyHHmmss = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
public static final DateFormat DF_ddMMyyyyMoscowTimeZone = new SimpleDateFormat("dd.MM.yyyy");
public static final DateFormat DF_ddMMyyyyHHmmssMoscowTimeZone = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
public static final DateTimeFormatter DTF_ddMMyyyy_Time_Zone = DateTimeFormat.forPattern("dd.MM.yyyy");
public static final DateTimeFormatter DTF_ddMMyyyyHHmmss_Time_Zone = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
public static final TimeZone TIME_ZONE_MOSCOW;
public static final int MOSCOW_OFFSET = 3;
static {
TIME_ZONE_MOSCOW = TimeZone.getTimeZone("Europe/Moscow");
DF_ddMMyyyyHHmmssMoscowTimeZone.setTimeZone(TIME_ZONE_MOSCOW);
DF_ddMMyyyyMoscowTimeZone.setTimeZone(TIME_ZONE_MOSCOW);
}
public static final DateFormat EMAIL_DATE_FORMAT = new SimpleDateFormat("«dd» MMMM yyyy г.", new Locale("ru", "RU"));
public static boolean period1ContainsPeriod2(Date start1, Date end1, Date start2, Date end2) {
return !start1.after(start2) && !end2.after(end1);
}
public static Date getLocalDateTime() {
return new LocalDateTime(DateTimeZone.UTC).toDate();
}
public static Date toDate(XMLGregorianCalendar c) {
if (c == null)
return null;
Date date = c.toGregorianCalendar().getTime();
// adjust time zone
return new DateTime(date, DateTimeZone.UTC).toLocalDateTime().toDate();
}
public static Date toDateWithoutTime(XMLGregorianCalendar c) {
if (c == null)
return null;
return c.toGregorianCalendar().getTime();
}
public static Date adjustTimeZone(Date date) {
return new DateTime(date, DateTimeZone.UTC).toLocalDateTime().toDate();
}
public static Date adjustTimeZoneBackToNormal(Date date) {
return new LocalDateTime(date.getTime()).toDateTime(DateTimeZone.UTC).toDate();
}
public static Date newDateWithAdjustedTimeZone() {
return new LocalDateTime(DateTimeZone.UTC).toDate();
}
public static XMLGregorianCalendar toXMLGregorianCalendar(Date d) {
if (d == null) {
return null;
}
try {
GregorianCalendar c = new GregorianCalendar();
c.setTime(d);
// adjust time zone
XMLGregorianCalendar newC = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
newC.setTimezone(getTimeZone("UTC").getRawOffset());
return newC;
} catch (DatatypeConfigurationException e) {
throw new RuntimeException(e);
}
}
public static XMLGregorianCalendar toXMLGregorianCalendarWithoutTimeZone(Date d) {
if (d == null) {
return null;
}
try {
GregorianCalendar c = new GregorianCalendar();
c.setTime(d);
// adjust time zone
XMLGregorianCalendar newC = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
newC.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
return newC;
} catch (DatatypeConfigurationException e) {
throw new RuntimeException(e);
}
}
public static XMLGregorianCalendar toXMLGregorianCalendarDateOnly(Date d) {
try {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH) + 1, gc.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
} catch (DatatypeConfigurationException e) {
throw new RuntimeException(e);
}
}
public static XMLGregorianCalendar toXMLGregorianCalendarTimeZone(Date d) {
try {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new DateTime(d).toGregorianCalendar());
} catch (DatatypeConfigurationException e) {
throw new RuntimeException(e);
}
}
public static String toyyyyMMddTHHmmssXXXString(Date d) {
if (d == null)
return "";
return DF_yyyyMMddTHHmmssXXX.format(d);
}
public static Date toyyyyMMddTHHmmssXXXDate(String s) {
if (StringUtil.isEmpty(s)) {
return null;
}
try {
return DF_yyyyMMddTHHmmssXXX.parse(s);
} catch (ParseException e) {
return null;
}
}
public static String toddMMyyyyString(Date d) {
if (d == null) {
return null;
}
return DF_ddMMyyyy.format(d);
}
public static String toddMMyyyyStringWithTimeZone(Date d, int offset) {
return Optional.ofNullable(d)
.map(date -> DTF_ddMMyyyy_Time_Zone.print(toDateTimeRelativelyMoscowWithTimeZone(date, offset)))
.orElse("");
}
public static String toddMMyyyyHHmmss(Date d) {
if (d == null) {
return "";
}
return DF_ddMMyyyyHHmmss.format(d);
}
public static String toddMMyyyyHHmmssWithTimeZone(Date d, int offset) {
return Optional.ofNullable(d)
.map(date -> DTF_ddMMyyyyHHmmss_Time_Zone.print(toDateTimeRelativelyMoscowWithTimeZone(d, offset)))
.orElse("");
}
public static String toRUddMMMMyyyy(DateTime dateTime) {
return dateTime.toString(PATTERN_ddMMMMyyyy, new Locale(LOCALE_RU));
}
public static String toRUddMMMMyyyy(String dateInddMMyyyy) {
return toRUddMMMMyyyy(DateTimeFormat.forPattern(PATTERN_ddMMyyyy).parseDateTime(dateInddMMyyyy));
}
public static Date toddMMyyyyDate(String s) {
if (StringUtil.isEmpty(s)) {
return null;
}
try {
return DF_ddMMyyyy.parse(s);
} catch (ParseException e) {
return null;
}
}
public static Date toyyyyMMddDate(String s) {
if (StringUtil.isEmpty(s)) {
return null;
}
try {
return DF_yyyyMMdd.parse(s);
} catch (ParseException e) {
return null;
}
}
public static String toyyyyMMddString(Date d) {
if (d == null)
return "";
return DF_yyyyMMdd.format(d);
}
public static String getNowInyyyyMMdd() {
return DF_yyyyMMdd.format(new Date());
}
public static Date toDateMoscowTimeZone(Date d) {
return new DateTime(d)
.withZoneRetainFields(DateTimeZone.UTC)
.withZone(DateTimeZone.forID("Europe/Moscow"))
.toLocalDateTime().toDate();
}
public static DateTime toDateTimeRelativelyMoscowWithTimeZone(Date d, int offset) {
return new DateTime(d)
.withZoneRetainFields(DateTimeZone.UTC)
.toDateTime(DateTimeZone.forOffsetHours(MOSCOW_OFFSET + offset))
.toDateTimeISO();
}
public static DateTime toDateTimeWithTimeZone(Date d, int offset) {
return new DateTime(d)
.withZoneRetainFields(DateTimeZone.UTC)
.toDateTime(DateTimeZone.forOffsetHours(offset))
.toDateTimeISO();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment