Created
December 1, 2018 12:59
-
-
Save Anjireddy4246/77d167a4f48a5b114145df50e15a4578 to your computer and use it in GitHub Desktop.
Date Conversion utilities in JAVA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.titan.infra.dateutils; | |
| import org.apache.commons.lang.StringUtils; | |
| import java.text.DateFormat; | |
| import java.text.ParseException; | |
| import java.text.SimpleDateFormat; | |
| import java.time.ZoneOffset; | |
| import java.time.ZonedDateTime; | |
| import java.time.format.DateTimeFormatter; | |
| import java.util.*; | |
| public class DateUtils { | |
| /** | |
| * Parse Date to String to give format and timezone | |
| * @param date | |
| * @param format | |
| * @param toTimeZone | |
| * @return | |
| */ | |
| public static String formatDateToString(Date date, String format, String toTimeZone) { | |
| // null check | |
| if (Objects.isNull(date)) { | |
| return null; | |
| } | |
| // create SimpleDateFormat object | |
| SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); | |
| // default system timezone if passed null or empty | |
| if (StringUtils.isBlank(toTimeZone)) { | |
| toTimeZone = Calendar.getInstance().getTimeZone().getID(); | |
| } | |
| // set timezone to SimpleDateFormat | |
| if (StringUtils.isNotBlank(toTimeZone)) { | |
| simpleDateFormat.setTimeZone(TimeZone.getTimeZone(toTimeZone)); | |
| } | |
| // set timezone to SimpleDateFormat | |
| simpleDateFormat.setTimeZone(TimeZone.getTimeZone(toTimeZone)); | |
| // return Date in required format with timezone as String | |
| String formattedDate = simpleDateFormat.format(date); | |
| return formattedDate; | |
| } | |
| /** | |
| * Convert Date from One timezone to other | |
| * @param dateFrom | |
| * @param fromTimeZone | |
| * @param toTimeZone | |
| * @return | |
| */ | |
| public static Date parseDate(Date dateFrom, String fromTimeZone, String toTimeZone) { | |
| String pattern = "yyyy/MM/dd HH:mm:ss"; | |
| // From Zone format | |
| SimpleDateFormat sdfFromSimpleDateFormatFrom = new SimpleDateFormat(pattern); | |
| sdfFromSimpleDateFormatFrom.setTimeZone(TimeZone.getTimeZone(fromTimeZone)); | |
| //To Zone format | |
| SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat(pattern); | |
| simpleDateFormatTo.setTimeZone(TimeZone.getTimeZone(toTimeZone)); | |
| Date dateTo = null; | |
| try { | |
| dateTo = sdfFromSimpleDateFormatFrom.parse(simpleDateFormatTo.format(dateFrom)); | |
| } catch (ParseException e) { | |
| e.printStackTrace(); | |
| } | |
| return dateTo; | |
| } | |
| /** | |
| * Format date as string | |
| * @param dateFrom | |
| * @param fromTimeZone | |
| * @param toTimeZone | |
| * @return | |
| */ | |
| public static Date parseDateToString(Date dateFrom, String fromTimeZone, String toTimeZone) { | |
| String pattern = "yyyy/MM/dd HH:mm:ss"; | |
| // From Zone format | |
| SimpleDateFormat sdfFromSimpleDateFormatFrom = new SimpleDateFormat(pattern); | |
| sdfFromSimpleDateFormatFrom.setTimeZone(TimeZone.getTimeZone(fromTimeZone)); | |
| //To Zone format | |
| SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat(pattern); | |
| simpleDateFormatTo.setTimeZone(TimeZone.getTimeZone(toTimeZone)); | |
| Date parsedDateTime = null; | |
| try { | |
| parsedDateTime = sdfFromSimpleDateFormatFrom.parse(simpleDateFormatTo.format(dateFrom)); | |
| } catch (ParseException e) { | |
| e.printStackTrace(); | |
| } | |
| return parsedDateTime; | |
| } | |
| /** | |
| * Get Current UTC Date | |
| * @return Current UTC Date | |
| */ | |
| public static Date getCurrentDate() { | |
| ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneOffset.UTC); | |
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss"); | |
| DateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); | |
| String dateStr = zonedDateTime.format(formatter); | |
| Date utcDate = null; | |
| try { | |
| utcDate = format.parse(dateStr); | |
| } catch (ParseException e) { | |
| e.printStackTrace(); | |
| } | |
| return utcDate; | |
| } | |
| // public static void main(String[] args) { | |
| // Date currentDate = getCurrentDate(); | |
| // System.out.println("DATE -> System Date in UTC: "+parseDate(currentDate, TimeZoneId.UTC, TimeZoneId.UTC)); | |
| // System.out.println("STRING -> System Date in UTC: "+parseDateToString(currentDate, TimeZoneId.UTC, TimeZoneId.UTC)); | |
| // | |
| // System.out.println("DATE -> System Date in PST: "+parseDate(currentDate, TimeZoneId.UTC, TimeZoneId.PST)); | |
| // System.out.println("STRING -> System Date in PST: "+parseDateToString(currentDate, TimeZoneId.UTC, TimeZoneId.PST)); | |
| // | |
| // System.out.println("DATE -> System Date in IST: "+parseDate(currentDate, TimeZoneId.UTC, TimeZoneId.IST)); | |
| // System.out.println("STRING -> System Date in IST: "+parseDateToString(currentDate, TimeZoneId.UTC, TimeZoneId.IST)); | |
| // } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.titan.infra.dateutils; | |
| import org.apache.commons.lang.StringUtils; | |
| import java.text.DateFormat; | |
| import java.text.ParseException; | |
| import java.text.SimpleDateFormat; | |
| import java.time.ZoneOffset; | |
| import java.time.ZonedDateTime; | |
| import java.time.format.DateTimeFormatter; | |
| import java.util.*; | |
| public class DateUtils { | |
| /** | |
| * Parse Date to String to give format and timezone | |
| * @param date | |
| * @param format | |
| * @param toTimeZone | |
| * @return | |
| */ | |
| public static String formatDateToString(Date date, String format, String toTimeZone) { | |
| // null check | |
| if (Objects.isNull(date)) { | |
| return null; | |
| } | |
| // create SimpleDateFormat object | |
| SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); | |
| // default system timezone if passed null or empty | |
| if (StringUtils.isBlank(toTimeZone)) { | |
| toTimeZone = Calendar.getInstance().getTimeZone().getID(); | |
| } | |
| // set timezone to SimpleDateFormat | |
| if (StringUtils.isNotBlank(toTimeZone)) { | |
| simpleDateFormat.setTimeZone(TimeZone.getTimeZone(toTimeZone)); | |
| } | |
| // set timezone to SimpleDateFormat | |
| simpleDateFormat.setTimeZone(TimeZone.getTimeZone(toTimeZone)); | |
| // return Date in required format with timezone as String | |
| String formattedDate = simpleDateFormat.format(date); | |
| return formattedDate; | |
| } | |
| /** | |
| * Convert Date from One timezone to other | |
| * @param dateFrom | |
| * @param fromTimeZone | |
| * @param toTimeZone | |
| * @return | |
| */ | |
| public static Date parseDate(Date dateFrom, String fromTimeZone, String toTimeZone) { | |
| String pattern = "yyyy/MM/dd HH:mm:ss"; | |
| // From Zone format | |
| SimpleDateFormat sdfFromSimpleDateFormatFrom = new SimpleDateFormat(pattern); | |
| sdfFromSimpleDateFormatFrom.setTimeZone(TimeZone.getTimeZone(fromTimeZone)); | |
| //To Zone format | |
| SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat(pattern); | |
| simpleDateFormatTo.setTimeZone(TimeZone.getTimeZone(toTimeZone)); | |
| Date dateTo = null; | |
| try { | |
| dateTo = sdfFromSimpleDateFormatFrom.parse(simpleDateFormatTo.format(dateFrom)); | |
| } catch (ParseException e) { | |
| e.printStackTrace(); | |
| } | |
| return dateTo; | |
| } | |
| /** | |
| * Format date as string | |
| * @param dateFrom | |
| * @param fromTimeZone | |
| * @param toTimeZone | |
| * @return | |
| */ | |
| public static Date parseDateToString(Date dateFrom, String fromTimeZone, String toTimeZone) { | |
| String pattern = "yyyy/MM/dd HH:mm:ss"; | |
| // From Zone format | |
| SimpleDateFormat sdfFromSimpleDateFormatFrom = new SimpleDateFormat(pattern); | |
| sdfFromSimpleDateFormatFrom.setTimeZone(TimeZone.getTimeZone(fromTimeZone)); | |
| //To Zone format | |
| SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat(pattern); | |
| simpleDateFormatTo.setTimeZone(TimeZone.getTimeZone(toTimeZone)); | |
| Date parsedDateTime = null; | |
| try { | |
| parsedDateTime = sdfFromSimpleDateFormatFrom.parse(simpleDateFormatTo.format(dateFrom)); | |
| } catch (ParseException e) { | |
| e.printStackTrace(); | |
| } | |
| return parsedDateTime; | |
| } | |
| /** | |
| * Get Current UTC Date | |
| * @return Current UTC Date | |
| */ | |
| public static Date getCurrentDate() { | |
| ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneOffset.UTC); | |
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss"); | |
| DateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); | |
| String dateStr = zonedDateTime.format(formatter); | |
| Date utcDate = null; | |
| try { | |
| utcDate = format.parse(dateStr); | |
| } catch (ParseException e) { | |
| e.printStackTrace(); | |
| } | |
| return utcDate; | |
| } | |
| // public static void main(String[] args) { | |
| // Date currentDate = getCurrentDate(); | |
| // System.out.println("DATE -> System Date in UTC: "+parseDate(currentDate, TimeZoneId.UTC, TimeZoneId.UTC)); | |
| // System.out.println("STRING -> System Date in UTC: "+parseDateToString(currentDate, TimeZoneId.UTC, TimeZoneId.UTC)); | |
| // | |
| // System.out.println("DATE -> System Date in PST: "+parseDate(currentDate, TimeZoneId.UTC, TimeZoneId.PST)); | |
| // System.out.println("STRING -> System Date in PST: "+parseDateToString(currentDate, TimeZoneId.UTC, TimeZoneId.PST)); | |
| // | |
| // System.out.println("DATE -> System Date in IST: "+parseDate(currentDate, TimeZoneId.UTC, TimeZoneId.IST)); | |
| // System.out.println("STRING -> System Date in IST: "+parseDateToString(currentDate, TimeZoneId.UTC, TimeZoneId.IST)); | |
| // } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment