Java Timestamp from convertDateToTimestamp(Date date)

Here you can find the source of convertDateToTimestamp(Date date)

Description

convert Date To Timestamp

License

Apache License

Declaration

public static Timestamp convertDateToTimestamp(Date date) 

Method Source Code

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

import java.sql.Timestamp;

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

import java.util.Date;

public class Main {
    public static Timestamp convertDateToTimestamp(Date date) {
        Timestamp ts = new Timestamp(date.getTime());
        return ts;
    }/*from  w  w w .j av  a 2  s.c o  m*/

    public static Date getTime() {
        try {
            return getDate("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate() {
        try {
            return getDate("yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate(String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = new Date(System.currentTimeMillis());
        return convertStringToDate(df.format(date), format);
    }

    public static Date convertStringToDate(String time, String format)
            throws ParseException {
        if (format == null) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(time);
    }
}

Related

  1. convertDateStringToTimestamp(String dateString, String format)
  2. convertDateToTimestamp(java.util.Date date)
  3. convertLongToTimestamp(Long millSec)
  4. convertSQLTimestamp(String s, String formatIn, String formatOut)
  5. convertString2TimeStamp(String dateString)