Java Parse Date parseDateStrShort(String date)

Here you can find the source of parseDateStrShort(String date)

Description

parse Date Str Short

License

Apache License

Declaration

public static Date parseDateStrShort(String date) 

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";
    private static final SimpleDateFormat DF_SHORT = new SimpleDateFormat("yy MM dd");

    public static Date parseDateStrShort(String date) {
        return parse(date, DF_SHORT);
    }// w w  w.  j  ava 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. parseDateString(String dateString)
  2. parseDateString(String pattern, String dateString)
  3. parseDateString(String s)
  4. parseDateString(TimeZone tz, String dateString)
  5. parseDateStringToDate(DateFormat format, String dateString)
  6. parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz)
  7. parseDateTime(@Nullable String s)
  8. parseDateTime(final String dateTime)
  9. parseDateTime(String _date)