Java TimeZone Format getTimeWithFormat(String stringDate, String dateFormat, DateTimeZone timeZone, Locale locale)

Here you can find the source of getTimeWithFormat(String stringDate, String dateFormat, DateTimeZone timeZone, Locale locale)

Description

e.g.

License

Apache License

Parameter

Parameter Description
time a parameter
dateFormat a parameter

Exception

Parameter Description
ASKFastCheckedException an exception

Declaration

public static DateTime getTimeWithFormat(String stringDate, String dateFormat, DateTimeZone timeZone,
        Locale locale) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class Main {
    private static final String serverTimezone = "Europe/Amsterdam";

    /**//from w w  w . j av  a 2s. c  om
     * e.g. 10:45:42 in HH:mm:ss or January 02, 2010 in "MMMM d, yyyy" etc to
     * its DateTime equivalent format
     * 
     * @param time
     * @param dateFormat
     * @return
     * @throws ASKFastCheckedException
     */
    public static DateTime getTimeWithFormat(String stringDate, String dateFormat, DateTimeZone timeZone,
            Locale locale) {
        locale = locale != null ? locale : Locale.ENGLISH;
        timeZone = timeZone != null ? timeZone : getServerDateTimeZone();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, locale);
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone.getID()));
            Date parsedDate = simpleDateFormat.parse(stringDate);
            return new DateTime(parsedDate, timeZone);
        } catch (ParseException e) {
            return null;
        }
    }

    public static DateTimeZone getServerDateTimeZone() {
        return DateTimeZone.forID(serverTimezone);
    }
}

Related

  1. getDefaultDateFormatWithTimeZone()
  2. getFormattedTime(Date time, String formatTime, String timezone)
  3. getSimpleDateFormat(TimeZone timeZone, String pattern)
  4. getString(Date dt, DateFormat df, TimeZone to)
  5. getTimeByZoneAndFormat(Date date, TimeZone zone, String format)
  6. getTimezoneFormattedString(Date date, String timezone)
  7. getUserToServerDateTimeString(TimeZone timeZone, int dateFormat, int timeFormat, String date)
  8. getZfgcTimeZoneDateFormat(String timezone)
  9. resolveTimeZone(SimpleDateFormat sdf, String timezone)