/** * Converts a given time from the specified {@link TimeZone} to the corresponding UTC (Coordinated Universal Time) time * * @param time the instant to convert, represented as the number of milliseconds that have elapsed since midnight, January 1, 1970 * @param from the original {@link TimeZone} * @return the UTC instant, represented as the number of milliseconds that have elapsed since midnight, January 1, 1970 */ public static long convertDateToUTC(long time, TimeZone from) { return convertDate(time, from, UTC_TIMEZONE); }
/** * Converts a given time from a {@link TimeZone} to another * * @param time the instant to convert, represented as the number of milliseconds that have elapsed since midnight, January 1, 1970 in the {@code from} {@link TimeZone} * @param from the original {@link TimeZone} * @param to the target {@link TimeZone} for the conversion * @return the long representation of the instant converted to the specified {@code to} {@link TimeZone} */ public static long convertDate(long time, TimeZone from, TimeZone to) { return time + getTimeZoneOffset(time, from, to); }
value = Instants.toUtcIso8601(date);
value = Instants.toUtcIso8601(date);
/** * Converts a given time from UTC (Coordinated Universal Time) to the corresponding time in the specified {@link TimeZone} * * @param time the UTC instant to convert, represented as the number of milliseconds that have elapsed since midnight, January 1, 1970 * @param to the target {@link TimeZone} for the conversion * @return the long representation of the instant converted to the specified {@link TimeZone} */ public static long convertDateToLocalTime(long time, TimeZone to) { return convertDate(time, UTC_TIMEZONE, to); }
/** * Creates an UTC-based {@Link Date} using the provided {@code year}. * Uses the current date and time, sets the specified {@code year} and returns the Date converted to a UTC timestamp. * * @param year the year to represent * @return the UTC-based {@link Date} */ public static Date of(int year) { GregorianCalendar cal = new GregorianCalendar(); cal.set(Calendar.YEAR, year); TimeZone fromTimeZone = cal.getTimeZone(); return new Date(convertDate(cal.getTimeInMillis(), fromTimeZone, UTC_TIMEZONE)); }
/** * Creates an UTC-based {@Link Date} from the provided {@code year} and {@code month}. * Uses the current date and time, sets the specified {@code year} and {@code month}, and returns the Date converted to a UTC timestamp. * * @param year the year to represent * @param month the month-of-year to represent, from 0 (January) to 11 (December) * @return the UTC-based {@link Date} */ public static Date of(int year, int month) { Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)"); GregorianCalendar cal = new GregorianCalendar(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); TimeZone fromTimeZone = cal.getTimeZone(); return new Date(convertDate(cal.getTimeInMillis(), fromTimeZone, UTC_TIMEZONE)); }
/** * Creates an UTC-based {@Link Date} from the provided {@code year}, {@code month} and {@code day} * Uses the current date and time, sets the specified {@code year}, {@code month} and {@code day}, and returns the Date converted to a UTC timestamp. * * @param year the year to represent * @param month the month-of-year to represent, from 0 (January) to 11 (December) * @param day the day-of-month to represent, from 1 to 31 * @return the UTC-based {@link Date} */ public static Date of(int year, int month, int day) { Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)"); Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31"); GregorianCalendar cal = new GregorianCalendar(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, day); TimeZone fromTimeZone = cal.getTimeZone(); return new Date(convertDate(cal.getTimeInMillis(), fromTimeZone, UTC_TIMEZONE)); }
/** * Creates an UTC-based {@Link Date} from the provided {@code year}, {@code month}, {@code day} and {@code hour}. * Uses the current date and time, sets the specified {@code year}, {@code month}, {@code day} and {@code hour}, and returns the Date converted to a UTC timestamp. * * @param year the year to represent * @param month the month-of-year to represent, from 0 (January) to 11 (December) * @param day the day-of-month to represent, from 1 to 31 * @param hour the hour-of-day to represent, from 0 to 23 * @return the UTC-based {@link Date} */ public static Date of(int year, int month, int day, int hour) { Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)"); Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31"); Assert.isTrue(0 <= hour && hour <= 23, "hour param must be a value from 0 to 23"); GregorianCalendar cal = new GregorianCalendar(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); TimeZone fromTimeZone = cal.getTimeZone(); return new Date(convertDate(cal.getTimeInMillis(), fromTimeZone, UTC_TIMEZONE)); }
/** * Creates an UTC-based {@Link Date} from the provided {@code year}, {@code month}, {@code day}, {@code hour} and {@code minute}. * Uses the current date and time, sets the specified {@code year}, {@code month}, {@code day}, {@code hour} and {@code minute}, and returns the Date converted to a UTC timestamp. * * @param year the year to represent * @param month the month-of-year to represent, from 0 (January) to 11 (December) * @param day the day-of-month to represent, from 1 to 31 * @param hour the hour-of-day to represent, from 0 to 23 * @param minute the minute-of-hour to represent, from 0 to 59 * @return the UTC-based {@link Date} */ public static Date of(int year, int month, int day, int hour, int minute) { Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)"); Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31"); Assert.isTrue(0 <= hour && hour <= 23, "hour param must be a value from 0 to 23"); Assert.isTrue(0 <= minute && minute <= 59, "minute param must be a value from 0 to 59"); GregorianCalendar cal = new GregorianCalendar(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); TimeZone fromTimeZone = cal.getTimeZone(); return new Date(convertDate(cal.getTimeInMillis(), fromTimeZone, UTC_TIMEZONE)); }
/** * Creates an UTC-based {@Link Date} from the provided {@code year}, {@code month}, {@code day}, {@code hour}, {@code minute} and {@code second}. * Uses the current date and time, sets the specified {@code year}, {@code month}, {@code day}, {@code hour}, {@code minute} and {@code second}, and returns the Date converted to a UTC timestamp. * * @param year the year to represent * @param month the month-of-year to represent, from 0 (January) to 11 (December) * @param day the day-of-month to represent, from 1 to 31 * @param hour the hour-of-day to represent, from 0 to 23 * @param minute the minute-of-hour to represent, from 0 to 59 * @param second the second-of-hour to represent, from 0 to 59 * @return the UTC-based {@link Date} */ public static Date of(int year, int month, int day, int hour, int minute, int second) { Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)"); Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31"); Assert.isTrue(0 <= hour && hour <= 23, "hour param must be a value from 1 to 23"); Assert.isTrue(0 <= minute && minute <= 59, "minute param must be a value from 0 to 59"); Assert.isTrue(0 <= second && second <= 59, "second param must be a value from 0 to 59"); GregorianCalendar cal = new GregorianCalendar(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, second); TimeZone fromTimeZone = cal.getTimeZone(); return new Date(convertDate(cal.getTimeInMillis(), fromTimeZone, UTC_TIMEZONE)); }
/** * Creates an UTC-based {@Link Date} from the provided {@code year}, {@code month}, {@code day}, {@code hour}, {@code minute} and {@code second}. * Uses the current date and time, sets the specified {@code year}, {@code month}, {@code day}, {@code hour}, {@code minute} and {@code second}, and returns the Date converted to a UTC timestamp. * * @param year the YEAR to represent * @param month the MONTH to represent, from 0 (January) to 11 (December) * @param day the DAY_OF_MONTH to represent, from 1 to 31 * @param hour the HOUR_OF_DAY to represent, from 0 to 23 * @param minute the MINUTE to represent, from 0 to 59 * @param second the SECOND to represent, from 0 to 59 * @param millisecond the MILLISECOND to represent, from 0 to 59 * @return the UTC-based {@link Date} */ public static Date of(int year, int month, int day, int hour, int minute, int second, int millisecond) { Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)"); Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31"); Assert.isTrue(0 <= hour && hour <= 23, "hour param must be a value from 1 to 23"); Assert.isTrue(0 <= minute && minute <= 59, "minute param must be a value from 0 to 59"); Assert.isTrue(0 <= second && second <= 59, "second param must be a value from 0 to 59"); GregorianCalendar cal = new GregorianCalendar(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, second); cal.set(Calendar.MILLISECOND, millisecond); TimeZone fromTimeZone = cal.getTimeZone(); return new Date(convertDate(cal.getTimeInMillis(), fromTimeZone, UTC_TIMEZONE)); }