Java String to Timestamp getStringToTimestamp(String string)

Here you can find the source of getStringToTimestamp(String string)

Description

return unix timestamp of specified string value in format: yyyy-MM-dd HH:mm:ss

License

Apache License

Parameter

Parameter Description
string string value to convert

Return

long value

Declaration

public static long getStringToTimestamp(String string) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParsePosition;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**/*from w  w  w .jav  a2 s.  c  o  m*/
     * return unix timestamp of specified string value in format: yyyy-MM-dd HH:mm:ss
     *
     * @param string string value to convert
     * @return long value
     */
    public static long getStringToTimestamp(String string) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ParsePosition parsePosition = new ParsePosition(0);
            Date date = simpleDateFormat.parse(string, parsePosition);
            return date.getTime();
        } catch (Exception e) {
            return -1;
        }
    }
}

Related

  1. calculateClientRequestId(String timestamp)
  2. getFileNameWithTimeStampInterposed(final String fileName)
  3. getTimeStamp(String date)
  4. getTimeStamp(String dateTime)
  5. getTimestamp(String dateValue, String pattern, TimeZone timezone)
  6. getTimestamp(String pattern)