Java Timestamp Format unixTimestamp(String date, String dateFormat)

Here you can find the source of unixTimestamp(String date, String dateFormat)

Description

unix Timestamp

License

Open Source License

Declaration

public static Long unixTimestamp(String date, String dateFormat) 

Method Source Code

//package com.java2s;

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

import java.util.Date;

public class Main {
    public static final DateFormat DEFAULT_DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Long unixTimestamp(String date) {
        return new Long(parseDate(date).getTime() / 1000);
    }/*from w ww  .j a  v a 2 s  .co  m*/

    public static Long unixTimestamp(String date, String dateFormat) {
        return new Long(parseDate(date, dateFormat).getTime() / 1000);
    }

    public static Long unixTimestamp(Date date) {
        return new Long(date.getTime() / 1000);
    }

    public static Date parseDate(String date, DateFormat df) {
        try {
            return df.parse(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static Date parseDate(String date, String dateFormat) {
        SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
        return parseDate(date, fmt);
    }

    public synchronized static Date parseDate(String date) {
        return parseDate(date, DEFAULT_DATETIME_FORMAT);
    }
}

Related

  1. timeToString(long timestamp, SimpleDateFormat format)
  2. toDateFromTimestamp(String timestamp, String format)
  3. toHumanTime(long timestamp, String format)
  4. toReadableTime(Long timestamp, String format)
  5. toTimeStamp(String dateTime, SimpleDateFormat format, int timeZoneOffset)