Java Timestamp Parse parseTimestamp2Long(String datestring, String datepattern, TimeZone timeZone)

Here you can find the source of parseTimestamp2Long(String datestring, String datepattern, TimeZone timeZone)

Description

parse date string with a given date pattern, return long type timestamp, unit:second precondition: it is better to call cubridmanager.CommonTool.validateTimestamp(String, String) first to void throwing an ParseException

License

Open Source License

Parameter

Parameter Description
datestring String date string eg: 2009-02-20 16:42:46
datepattern String date pattern eg: yyyy-MM-dd HH:mm:ss
timeZone TimeZone if null ,the default will be GMT

Exception

Parameter Description
ParseException e

Return

long timestamp

Declaration

private static long parseTimestamp2Long(String datestring,
        String datepattern, TimeZone timeZone) throws ParseException 

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.Locale;
import java.util.TimeZone;

public class Main {
    /**/*from   w  ww.j  av a  2  s  . co  m*/
     * parse date string with a given date pattern, return long type timestamp,
     * unit:second
     * 
     * precondition: it is better to call
     * cubridmanager.CommonTool.validateTimestamp(String, String) first to void
     * throwing an ParseException
     * 
     * @param datestring String date string eg: 2009-02-20 16:42:46
     * @param datepattern String date pattern eg: yyyy-MM-dd HH:mm:ss
     * @param timeZone TimeZone if null ,the default will be GMT
     * @return long timestamp
     * @throws ParseException e
     */
    private static long parseTimestamp2Long(String datestring,
            String datepattern, TimeZone timeZone) throws ParseException {
        DateFormat formatter = getDateFormat(datepattern, Locale.US,
                timeZone);
        Date date = formatter.parse(datestring);
        return date.getTime();
    }

    /**
     * return a standard DateFormat instance, which has a given Local, TimeZone,
     * and with a strict check
     * 
     * @param datepattern String
     * @param locale Locale
     * @param timeZone TimeZone
     * @return DateFormat
     */
    public static DateFormat getDateFormat(String datepattern,
            Locale locale, TimeZone timeZone) {
        DateFormat formatter = new SimpleDateFormat(datepattern, locale);
        formatter.setLenient(false);
        if (timeZone == null) {
            formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        } else {
            formatter.setTimeZone(timeZone);
        }
        return formatter;
    }
}

Related

  1. parseTimeStamp(String timeStamp)
  2. parseTimestamp(String timestamp, String dateFormat, Locale locale)
  3. parseTimeStamp(String timeStampString)
  4. parseTimestamp(String value)
  5. parseTimestamp(String value)
  6. parseTimestampDirectory(final String stamp)
  7. parseTimestampToString(Timestamp stamp, String dateFormat)
  8. parseToDate(String timestamp, String pattern)
  9. str2Timestamp(String str)