Java String to Timestamp getTimeStamp(String pattern, String strDate)

Here you can find the source of getTimeStamp(String pattern, String strDate)

Description

get Time Stamp

License

Open Source License

Declaration

public static long getTimeStamp(String pattern, String strDate) 

Method Source Code


//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static String datePattern = "MM-dd-yyyy";

    public static long getTimeStamp(String pattern, String strDate) {
        long returnTimeStamp = 0;
        Date aDate = null;/*  w w w. j a  va 2  s. co m*/
        try {
            aDate = convertStringToDate(pattern, strDate);
        } catch (ParseException pe) {
            aDate = null;
        }
        if (aDate == null) {
            returnTimeStamp = 0;
        } else {
            returnTimeStamp = aDate.getTime();
        }
        return returnTimeStamp;
    }

    /**
     * This method generates a string representation of a date/time
     * in the format you specify on input
     *
     * @param aMask the date pattern the string is in
     * @param strDate a string representation of a date
     * @return a converted Date object
     * @see java.text.SimpleDateFormat
     * @throws ParseException
     */
    public static final Date convertStringToDate(String aMask, String strDate) throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            return null;
        }

        return (date);
    }

    /**
     * This method converts a String to a date using the datePattern
     * @param strDate the date to convert (in format MM/dd/yyyy)
     * @return a date object
     * @throws ParseException
     */
    public static Date convertStringToDate(String strDate) throws ParseException {
        Date aDate = null;

        try {

            aDate = convertStringToDate(datePattern, strDate);
        } catch (ParseException pe) {
            //log.error("Could not convert '" + strDate
            //          + "' to a date, throwing exception");
            pe.printStackTrace();
            return null;

        }
        return aDate;
    }
}

Related

  1. getStringToTimestamp(String string)
  2. getTimeStamp(String date)
  3. getTimeStamp(String dateTime)
  4. getTimestamp(String dateValue, String pattern, TimeZone timezone)
  5. getTimestamp(String pattern)
  6. getTimeStamp(String style, Date date)
  7. getTimestamp(String timeStampStr, String logRundateIdStr)
  8. getTimestamp(String timezone, String dateTime, String pattern)
  9. getTimestampFromDateString(String date)