Java Parse Date Pattern YYYY parseString2Long(String strDate)

Here you can find the source of parseString2Long(String strDate)

Description

parse String Long

License

Open Source License

Declaration

public static Long parseString2Long(String strDate) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    public static String FORMAT_STR_TIME = "yyyy-MM-dd HH:mm:ss";
    public static DateFormat FORMAT_TIME = new SimpleDateFormat(FORMAT_STR_TIME);
    private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static TimeZone timeZone = TimeZone.getTimeZone(TimeZone.getDefault().getID());

    public static Long parseString2Long(String strDate) {
        return new Long(parseDate(strDate, FORMAT_STR_TIME).getTime());
    }/*from ww  w  .  j  a va2  s.co  m*/

    public static Date parseDate(String strDate, String format) {
        try {
            if (strDate == null || strDate.equals(""))
                return null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(strDate);
        } catch (Exception e) {
        }
        return new Date();
    }

    public static Date parseDate(String strDate, String format, boolean seccessFlag) {

        try {
            if (strDate == null || strDate.equals(""))
                return null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(strDate);
        } catch (Exception e) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                return simpleDateFormat.parse(strDate);
            } catch (Exception ex) {
                try {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
                    return simpleDateFormat.parse(strDate);
                } catch (Exception ee) {

                }
            }

        }
        return null;
    }

    public static String parse(Date date) {
        String str = "";
        try {
            FORMAT_TIME.setTimeZone(timeZone);
            TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
            str = FORMAT_TIME.format(date);
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return str;
    }

    public static Date format(String time) throws ParseException {
        if (time == null) {
            return null;
        }
        sf.setTimeZone(timeZone);
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        return sf.parse(time);
    }
}

Related

  1. parseString(String dateStr)
  2. parseString(String strDate)
  3. parseString2Calendar(String date)
  4. parseString2Date(String date)
  5. parseString2Date(String sDate, String format)
  6. parseStringAsDate(String pattern, String value)
  7. parseStringDate(String dateAsString)
  8. parseStringDateToRmFormat(String date)
  9. parseStringForDate(String dateString)