Java XML Date toTimeZone(String date, String toTimeZone)

Here you can find the source of toTimeZone(String date, String toTimeZone)

Description

Convert ISO8601 date to date in given toTimeZone .

License

Open Source License

Parameter

Parameter Description
date a parameter
toTimeZone a parameter

Exception

Parameter Description
ParseException On date parse error.

Declaration

public static String toTimeZone(String date, String toTimeZone) throws ParseException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**/*from www  .  ja  va2s .  co m*/
     * Convert ISO8601 {@code date} to date in given {@code toTimeZone}.
     * 
     * @param date
     * @param toTimeZone
     * @return
     * @throws ParseException
     *             On {@code date} parse error.
     */
    public static String toTimeZone(String date, String toTimeZone) throws ParseException {
        // DateFormat dateFormat = new SimpleDateFormat(datePattern);
        // dateFormat.setTimeZone(TimeZone.getTimeZone(fromTimeZone));
        // return dateFormat.format(parseDateISO8601(date));
        return toStringISO8601(parseDateISO8601(date), toTimeZone);
    }

    /**
     * Converts {@code fromCalendar}'s date, time & time zone to a
     * {@link Calendar} with {@link toTimeZone} time zone.
     * 
     * @param fromCalendar
     *            Input date, time & time zone.
     * @param toTimeZone
     *            Target time zone to convert {@code fromCalendar} to.
     * @return
     */
    public static Calendar toTimeZone(Calendar fromCalendar, TimeZone toTimeZone) {
        Calendar calendar = Calendar.getInstance(toTimeZone);
        calendar.setTimeInMillis(fromCalendar.getTimeInMillis());
        return calendar;
    }

    /**
     * @see #toTimeZone(Calendar, TimeZone)
     */
    public static Calendar toTimeZone(Calendar fromCalendar, String toTimeZone) {
        return toTimeZone(fromCalendar, TimeZone.getTimeZone(toTimeZone));
    }

    public static String toStringISO8601(Date date, String timeZone) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.setTimeZone(TimeZone.getTimeZone(timeZone));
        return javax.xml.bind.DatatypeConverter.printDateTime(cal);
    }

    public static String toStringISO8601(Calendar cal) throws ParseException {
        return javax.xml.bind.DatatypeConverter.printDateTime(cal);
    }

    public static Date parseDateISO8601(String date) throws ParseException {
        Calendar cal = javax.xml.bind.DatatypeConverter.parseDateTime(date);
        return cal.getTime();
    }
}

Related

  1. printDateTime(final Date date)
  2. printDateUpdatedType(Calendar value)
  3. string2millis(String lexicalXSDDateTime)
  4. toDate(String date)
  5. toJavaDate(String dateStr)