com.hangum.tadpole.engine.utils.TimeZoneUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.hangum.tadpole.engine.utils.TimeZoneUtil.java

Source

/*******************************************************************************
 * Copyright (c) 2016 hangum.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     hangum - initial API and implementation
 ******************************************************************************/
package com.hangum.tadpole.engine.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
//import org.joda.time.DateTime;
//import org.joda.time.DateTimeZone;
//import org.joda.time.format.DateTimeFormat;
//import org.joda.time.format.DateTimeFormatter;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.hangum.tadpole.preference.define.GetAdminPreference;
import com.hangum.tadpole.session.manager.SessionManager;

/**
 * utc ?  ? ? ?   ?.
 * 
 * @author hangum
 *
 */
public class TimeZoneUtil {
    private static final Logger logger = Logger.getLogger(TimeZoneUtil.class);

    /**
     * timezone list
     * 
     * @return
     */
    public static Set<String> getTimezoneList() {
        return DateTimeZone.getAvailableIDs();
    }

    /**
     * db? timezone ?  ??  .
     * 
     * @param date
     * @return
     */
    public static String dateToStr(Date date) {
        String dbTimeZone = GetAdminPreference.getDBTimezone();

        // db? timezone    .
        if (StringUtils.isEmpty(dbTimeZone)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        } else {
            //   UTC   . 
            DateTime targetDateTime = new DateTime(date).withZone(DateTimeZone.forID(SessionManager.getTimezone()));
            String strPretty = targetDateTime.toString(prettyDateTimeFormater());

            //          if(logger.isDebugEnabled()) {
            //             logger.debug(String.format("[SessionManager dbTimezone] %s => %s", SessionManager.getTimezone(), targetDateTime));
            //             logger.debug(String.format("[strPretty] %s", strPretty));
            //             logger.debug("===============================================================");
            //          }

            return strPretty;
        }
    }

    /**
     * ?  ? ?  .
     * 
     * @param localTimeMills
     * @return
     */
    public static long chageTimeZone(long localTimeMills) {
        String dbTimeZone = GetAdminPreference.getDBTimezone();
        // db? timezone    .
        if (StringUtils.isEmpty(dbTimeZone)) {
            return localTimeMills;
        } else {
            //   UTC   . 
            DateTime sourceDateTime = new DateTime(localTimeMills,
                    DateTimeZone.forID(SessionManager.getTimezone()));
            DateTime targetDateTime = sourceDateTime.withZone(DateTimeZone.forID(dbTimeZone));
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("[SessionManager dbTimezone] %s => %s => %s",
                        SessionManager.getTimezone(), localTimeMills, sourceDateTime.toString()));
                logger.debug(String.format("[targetDateTime] %s => %s", targetDateTime.getMillis(),
                        targetDateTime.toString()));
                logger.debug("===============================================================");
            }

            return targetDateTime.getMillis();
        }
    }

    /**
     * prettyDate
     * @param date
     * @return
     */
    public static DateTimeFormatter prettyDateTimeFormater() {
        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        return dtf;
    }

    //   /**
    //    * sample date time
    //    * @param args
    //    */
    //   public static void main(String[] args) {
    ////      DateTimeZone timeZoneUTC = DateTimeZone.forID( "UTC" );
    ////      DateTimeZone timeZoneLondon = DateTimeZone.forID( "Europe/London" );
    ////      DateTimeZone timeZoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );
    ////      DateTimeZone timeZoneNewYork = DateTimeZone.forID( "America/New_York" );
    //      DateTimeZone timeAsiaSeoul = DateTimeZone.forID( "Asia/Seoul" );
    //      
    //      DateTime nowSeoul = DateTime.now( timeAsiaSeoul );
    //      System.out.println(String.format("=%s:%s", "nowSeoul", nowSeoul) );
    //
    //      
    //      DateTime nowUtcnowSeoul = nowSeoul.withZone( DateTimeZone.UTC );  // Built-in constant for UTC.
    //      System.out.println(String.format("=%s:%s", "nowUtcnowSeoul", nowUtcnowSeoul) );
    //   }

    //   /**
    //    * defaultti
    //    * @param date
    //    * @return
    //    */
    //   private static String defaultTime(Date date) {
    //      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //      return sdf.format(date);
    //   }

}