Java Parse Date Pattern YYYY parse(String time, String df)

Here you can find the source of parse(String time, String df)

Description

Parse to GMT date

License

Apache License

Parameter

Parameter Description
time a parameter
df a parameter

Declaration

public static Date parse(String time, String df) 

Method Source Code

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static final String DEFAULT_FORMAT = "EEE MMM dd HH:mm:ss z yyyy";

    /**//from   w  w w .  j  av a  2  s.c  o  m
     * Parse to GMT date
     * @param time
     * @param df
     * @return
     */
    public static Date parse(String time, SimpleDateFormat df) {
        return parse(time, df, null);
    }

    public static Date parse(String time, SimpleDateFormat df, TimeZone timeZone) {

        try {
            Date date = df.parse(time);
            if (timeZone != null) {
                date = fromLocalTZ(date, timeZone);
            }
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * Parse to GMT date
     * @param time
     * @param df
     * @return
     */
    public static Date parse(String time, String df) {
        return parse(time, df, null);
    }

    public static Date parse(String time, String df, TimeZone timeZone) {
        try {
            Date date = new SimpleDateFormat(df).parse(time);
            if (timeZone != null) {
                date = fromLocalTZ(date, timeZone);
            }
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parse(String time, String[] dfs, TimeZone timeZone) {
        for (String df : dfs) {
            Date parse = parse(time, df, timeZone);
            if (parse != null) {
                return parse;
            }
        }
        return null;
    }

    public static Date parse(String dateStr) {
        return parse(dateStr, DEFAULT_FORMAT);
    }

    public static Date fromLocalTZ(Date time, TimeZone localTimeZone) {
        return new Date(time.getTime() - (localTimeZone.getRawOffset() - TimeZone.getDefault().getRawOffset()));
    }
}

Related

  1. parse(String string)
  2. parse(String stringData)
  3. parse(String text)
  4. parse(String text)
  5. parse(String time)
  6. parse1(String DateString, String dateTimeSeparator)
  7. parse2DateString(String date)
  8. parse_default(String time)
  9. parseApplePurchaseDate(String dateStr)