Java TimeZone Create getUserTimeZoneDate(String userTimezone)

Here you can find the source of getUserTimeZoneDate(String userTimezone)

Description

get User Time Zone Date

License

Open Source License

Declaration

private static Date getUserTimeZoneDate(String userTimezone) 

Method Source Code

//package com.java2s;
/*// w ww.  j a v  a 2  s.co m
 * Copyright (C) 2012  Krawler Information Systems Pvt Ltd
 * All rights reserved.
 * 
 * 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.
*/

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    private static Date getUserTimeZoneDate(String userTimezone) { // use for unit testing of time zone related to creation date of modules
                                                                   // records
        Date date = new Date();
        System.out.println(date);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
        // SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone(userTimezone)); // replace timezine
                                                             // with user
                                                             // timezone
        String datestr = sdf.format(date);
        System.out.println(datestr);
        String[] datearr = datestr.split(" ");

        // datearr[4] contains user time zone format which is replaced by space.
        // so that new date function use system time zone format which is
        // already set at user session creation time
        String newstr = datestr.replace(datearr[4], " ");

        Date newdate = new Date(newstr);
        System.out.println(newdate);
        return newdate;
    }
}

Related

  1. getTimeZoneList()
  2. getTimezoneList()
  3. getTimeZoneMap()
  4. getTimeZoneOfTenant()
  5. getTotalTimeZoneOffset(TimeZone zone)
  6. testSimpleTimeZone()
  7. toTimeZone(int gmtOffset)
  8. toTimeZone(String value)
  9. toTimeZone(String zone)