Statyczne metody do formatowania i konwersji dat.
package dt12.static_methods; import java.text.*; import java.util.*; class DateUtil{ private static final String SQL_DATE_FORMAT = " yyyy-mm-dd"; private static final String RFC_1123_FORMAT = "G, dd MMM yyyy hh:mm:ss z"; private static final String RFC_3339_FORMAT = "yyyy-mm-dd'T'hh:mm:ssZ"; private DateUtil(){} //---------------------- formatowanie dat ----------------------------- /** * Data stosowana np. w <time></time> * @param cal - data * @return - data w postaci sformatowaego stringu */ public static String formatAsRfc3339(GregorianCalendar cal) { Date date = new Date(cal.getTimeInMillis()); Locale loc = new Locale("en_EN"); SimpleDateFormat form1 = new SimpleDateFormat(RFC_3339_FORMAT, loc); return form1.format(date); } public static String formatAsRfc3339(Date date) { Locale loc = new Locale("en_EN"); SimpleDateFormat form1 = new SimpleDateFormat(RFC_3339_FORMAT, loc); return form1.format(date); } public static String formatAsRfc3339(java.sql.Date date) { Locale loc = new Locale("en_EN"); SimpleDateFormat form1 = new SimpleDateFormat(RFC_3339_FORMAT, loc); return form1.format(date); } /** * Data stosowana np. w HTML5 meta-equiv="expires" * @param cal - data * @return - data w postaci sformatowaego stringu */ public static String formatAsRfc1123(GregorianCalendar cal) { Date date = new Date(cal.getTimeInMillis()); SimpleDateFormat form = new SimpleDateFormat(RFC_1123_FORMAT, new Locale("en_EN")); return form.format(date); } /** * Data stosowana np. w HTML5 meta-equiv="expires" * @param date - data * @return - data w postaci sformatowaego stringu */ public static String formatAsRfc1123(Date date) { SimpleDateFormat form = new SimpleDateFormat(RFC_1123_FORMAT, new Locale("en_EN")); return form.format(date); } /** * Data stosowana np. w HTML5 meta-equiv="expires" * @param date - data * @return - data w postaci sformatowaego stringu */ public static String formatAsRfc1123(java.sql.Date date) { SimpleDateFormat form = new SimpleDateFormat(RFC_1123_FORMAT, new Locale("en_EN")); return form.format(date); } public static String formatAsSqlDate(Date date) { SimpleDateFormat form = new SimpleDateFormat(SQL_DATE_FORMAT, new Locale("en_EN")); return form.format(date); } public static String formatAsSqlDate(java.sql.Date date) { return date.toString(); } public static String formatAsSqlDate(GregorianCalendar cal) { return toSqlDate(cal).toString(); } public static String formatAsSqlDate(int year, int month, int day) { StringBuilder sb = new StringBuilder(); if(year > 999){ sb.append(year); } else{ throw new IllegalArgumentException( "Rok powinien być liczbą 4-cyfrową"); } sb.append("-"); if(month < 10){ sb.append("0"); sb.append(month); } else{ sb.append(month); } sb.append("-"); if(day > 9){ sb.append(day); } else{ sb.append("0"); sb.append(day); } return sb.toString(); } //------------------------ przekształcenia dat między sobą ------------------ public static Date toUtilDate(GregorianCalendar gc) { return new Date(gc.getTimeInMillis()); } public static Date toUtilDate(java.sql.Date date) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); return new Date(gc.getTimeInMillis()); } /** * Data musi zawierać rok 4-cyfrowy. Miesiące mogą być 1 * lub 2 cyfrowe, oddzielone '-' lub '/' * @param date - data * @return zwraca nowy obiekt Date */ public static Date toUtilDate(String date) { int firstDelim = -1; int secondDelim = -1; if(date.contains("-")){ firstDelim = date.indexOf('-'); secondDelim = date.indexOf('-', firstDelim + 1); } if(date.contains("/")){ firstDelim = date.indexOf('/'); secondDelim = date.indexOf('/', firstDelim + 1); } String dy = date.substring(0, firstDelim); if(firstDelim == 4){} else if(firstDelim == 2){ dy = "20".concat(dy); } else{ throw new IllegalArgumentException("Nieprawidłowa data"); } String dm = date.substring(firstDelim + 1, secondDelim); if(dm.length() == 1 || dm.length() == 2){} else{ throw new IllegalArgumentException("Nieprawidłowa data"); } //- String dd = date.substring(secondDelim + 1); if(dd.length() == 1 || dd.length() == 2){} else{ throw new IllegalArgumentException("Nieprawidłowa data"); } GregorianCalendar gc = new GregorianCalendar(Integer.parseInt(dy), Integer.parseInt(dm + 1), Integer.parseInt(dd)); return new Date(gc.getTimeInMillis()); } public static Date toUtilDate(int year, int month, int day) { GregorianCalendar gc = new GregorianCalendar(year, month - 1, day); return new Date(gc.getTimeInMillis()); } private static java.sql.Date toSqlDate(GregorianCalendar gc) { return new java.sql.Date(gc.getTimeInMillis()); } public static java.sql.Date toSqlDate(Date date) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); return new java.sql.Date(gc.getTimeInMillis()); } /** * Data musi zawierać rok 4-cyfrowy. Miesiące mogą być 1 * lub 2 cyfrowe, oddzielone '-' lub '/' * @param date - data * @return obiekt Date */ public static java.sql.Date toSqlDate(String date) { int firstDelim = -1; int secondDelim = -1; StringBuilder sb = new StringBuilder(); if(date.contains("-")){ firstDelim = date.indexOf('-'); secondDelim = date.indexOf('-', firstDelim + 1); } if(date.contains("/")){ firstDelim = date.indexOf('/'); secondDelim = date.indexOf('/', firstDelim + 1); } String dy = date.substring(0, firstDelim); if(firstDelim == 4){ sb.append(dy); sb.append("-"); } else if(firstDelim == 2){ sb.append("20"); sb.append(dy); sb.append("-"); } else{ throw new IllegalArgumentException("Nieprawidłowa data"); } String dm = date.substring(firstDelim + 1, secondDelim); if(dm.length() == 1){ sb.append("0"); sb.append(dm); sb.append("-"); } else if(dm.length() == 2){ sb.append(dm); sb.append("-"); } else{ throw new IllegalArgumentException("Nieprawidłowa data"); } //- String dd = date.substring(secondDelim + 1); if(dd.length() == 1){ sb.append("0"); sb.append(dd); } else if(dd.length() == 2){ sb.append(dd); } else{ throw new IllegalArgumentException("Nieprawidłowa data"); } return java.sql.Date.valueOf(sb.toString()); } public static java.sql.Date toSqlDate(int year, int month, int day) { GregorianCalendar gc = new GregorianCalendar(year, month - 1, day); return new java.sql.Date(gc.getTimeInMillis()); } /** * Przelicza datę na GC. Działa dla dat od 1 stycznia 1900 r. * @param date - data * @return nowy GregorianCalendar */ public static GregorianCalendar toGregCal(Date date) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); return gc; } /** * Przelicza datę na GC. Działa dla dat od 1 stycznia 1900 r. * @param date data * @return nowy GregorianCalendar */ public static GregorianCalendar toGregCal(java.sql.Date date) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); return gc; } /** * Data musi zawierać rok 4-cyfrowy. Miesiące mogą być 1 * lub 2 cyfrowe, oddzielone '-' lub '/' * @param date data * @return nowy GregorianCalendar */ public static GregorianCalendar toGregCal(String date) { int firstDelim = -1; int secondDelim = -1; if(date.contains("-")){ firstDelim = date.indexOf('-'); secondDelim = date.indexOf('-', firstDelim + 1); } if(date.contains("/")){ firstDelim = date.indexOf('/'); secondDelim = date.indexOf('/', firstDelim + 1); } String dy; if(firstDelim == 4){ dy = date.substring(0, firstDelim); } else if(firstDelim == 2){ dy = "20".concat(date.substring(0, firstDelim)); } else{ throw new IllegalArgumentException("Nieprawidłowa data"); } String dm = date.substring(firstDelim + 1, secondDelim); String dd = date.substring(secondDelim + 1); int year = Integer.parseInt(dy); int month = Integer.parseInt(dm); int day = Integer.parseInt(dd); return new GregorianCalendar(year, month - 1, day); } }