Here you can find the source of parseTimestamp2Long(String datestring, String datepattern, TimeZone timeZone)
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 |
Parameter | Description |
---|---|
ParseException | e |
private static long parseTimestamp2Long(String datestring, String datepattern, TimeZone timeZone) throws ParseException
//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; } }