Java tutorial
/* * Copyright (C) 2012-2014 Open Source Consulting, Inc. All rights reserved by Open Source Consulting, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Revision History * Author Date Description * --------------- ---------------- ------------ * Sang-cheon Park 2014. 8. 20. First Draft. */ package com.athena.peacock.common.core.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; import org.apache.commons.lang.StringUtils; import org.springframework.util.Assert; /** * <pre> * * </pre> * * @author Sang-cheon Park * @version 1.0 */ public class XMLGregorialCalendarUtil { public static String DEFAULT_DATE_PATTERN = "yyyy.MM.dd HH:mm:ss"; /** * <pre> * ?? ? . * , pattern? ?? 'yyyyMMddHHmmss' . * </pre> * * @param date Date ? * @return ? */ public static String getFormattedDate(String pattern, Date date) { if (date == null) { return null; } if (StringUtils.isEmpty(pattern)) { pattern = DEFAULT_DATE_PATTERN; } return new SimpleDateFormat(pattern).format(date); }// end of getFormattedDate() /** * <pre> * XMLGregorialCalendar ? GMT ?? GMT+09:00 DEFAULT_DATE_PATTERN? ?? ? . * </pre> * @param gmtString * @return */ public static String convertXmlGregorianCalendarToFormattedString(String gmtString) { Assert.notNull(gmtString, "gmtString must not be null."); Calendar calendar = null; try { calendar = gmtStringToGregorianCalendar(gmtString); } catch (DatatypeConfigurationException e) { throw new IllegalArgumentException( " . : [GMT : " + gmtString + "]"); } return getFormattedDate(DEFAULT_DATE_PATTERN, calendar.getTime()); }//end of convertXmlGregorianCalendarToFormattedString() /** * <pre> * YYYYMMDDHH24MISS? ?? ?? XMLGregorialCalendar ? GMT(GMT+09:00) ? . * </pre> * @param dateString * @return */ public static String convertFormattedStringToXmlGregorianCalendarStr(String dateString) { Assert.notNull(dateString, "dateString must not be null."); if (dateString.length() < 14) { dateString = StringUtils.rightPad(dateString, 14, '0'); } Assert.isTrue(dateString.length() == 14, "dateString's length must be 14."); GregorianCalendar cal = null; XMLGregorianCalendar calender = null; try { cal = convertTimezone(getDate(dateString), TimeZone.getTimeZone("ROK")); calender = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); return calender.toString(); } catch (Exception e) { throw new IllegalArgumentException( " . : [Date : " + dateString + " ]"); } }//end of convertFormattedStringToXmlGregorianCalendar() /** * <pre> * YYYYMMDDHH24MISS? ?? ?? XMLGregorialCalendar . * </pre> * @param dateString * @return */ public static XMLGregorianCalendar convertFormattedStringToXmlGregorianCalendar(String dateString) { Assert.notNull(dateString, "dateString must not be null."); GregorianCalendar cal = null; XMLGregorianCalendar calender = null; try { cal = convertTimezone(getDate(dateString), TimeZone.getTimeZone("ROK")); calender = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); } catch (Exception e) { throw new IllegalArgumentException( " . : [Date : " + dateString + " ]"); } return calender; }//end of convertFormattedStringToXmlGregorianCalendar() /** * <pre> * YYYY-MM-DDTHH:MI:SS.SSS+09:00 ? ?(:2007-02-13T10:25:05.986+07:00) ? GregorianCalendar ?. * </pre> * * @param stringTypeDate YYYY-MM-DDTHH:MI:SS.SSS+09:00 ? ?(:2007-02-13T10:25:05.986+07:00) * @return ? GregorianCalendar */ private static GregorianCalendar gmtStringToGregorianCalendar(String stringTypeDate) throws DatatypeConfigurationException { String yyyy = stringTypeDate.substring(0, 4); String mm = stringTypeDate.substring(5, 7); String dd = stringTypeDate.substring(8, 10); String hh = "00"; String mi = "00"; String ss = "00"; String ms = "00"; String tz = null; if (stringTypeDate.length() > 23) { hh = stringTypeDate.substring(11, 13); mi = stringTypeDate.substring(14, 16); ss = stringTypeDate.substring(17, 19); ms = stringTypeDate.substring(20, 23); tz = stringTypeDate.substring(23); } else { tz = stringTypeDate.substring(10); //tz = "+09:00"; } if (tz.equals("Z")) { tz = "UTC"; } else { tz = "GMT" + tz; } int iyyyy = Integer.parseInt(yyyy); int imm = Integer.parseInt(mm) - 1; int idd = Integer.parseInt(dd); int ihh = Integer.parseInt(hh); int imi = Integer.parseInt(mi); int iss = Integer.parseInt(ss); int ims = Integer.parseInt(ms); Calendar c = Calendar.getInstance(); c.setTimeZone(TimeZone.getTimeZone(tz)); c.set(iyyyy, imm, idd, ihh, imi, iss); c.set(Calendar.MILLISECOND, ims); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(c.getTime()); cal.setTimeZone(TimeZone.getTimeZone("ROK")); return cal; }//end of gmtStringToGregorianCalendar() /** * <pre> * ?? 'yyyyMMddHHmmss' Date . * </pre> * * @param date ? * @return ? Date ? */ private static Date getDate(String date) throws ParseException { if (date == null) { return null; } int yyyy = Integer.parseInt(date.substring(0, 4)); int mm = Integer.parseInt(date.substring(4, 6)); int dd = Integer.parseInt(date.substring(6, 8)); int hh = Integer.parseInt(date.substring(8, 10)); int mi = Integer.parseInt(date.substring(10, 12)); int ss = Integer.parseInt(date.substring(12, 14)); if (yyyy <= 1900 || yyyy >= 2999) { throw new ParseException("Invalid year.", yyyy); } if (mm < 1 || mm > 12) { throw new ParseException("Invalid month.", mm); } if (dd < 1 || dd > 31) { throw new ParseException("Invalid Day.", dd); } if (hh < 0 || hh > 23) { throw new ParseException("Invalid hour.", hh); } if (mi < 0 || mi > 59) { throw new ParseException("Invalid minute.", mi); } if (ss < 0 || ss > 59) { throw new ParseException("Invalid second.", ss); } return new SimpleDateFormat(DEFAULT_DATE_PATTERN).parse(date); }//end of getDate() /** * <pre> * ? GMT+09:00? UTC . * </pre> * @param date * @param tz * @return ? * @throws DatatypeConfigurationException */ private static GregorianCalendar convertTimezone(Date date, TimeZone tz) throws DatatypeConfigurationException { Assert.notNull(date, "date must not be null."); Assert.notNull(tz, "tz must not be null."); GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("ROK")); cal.setTime(date); cal.setTimeZone(tz); return cal; }//end of convertTimezone() } // end of XMLGregorialCalendarUtil.java