Java Timestamp Field getNowTimeStamp()

Here you can find the source of getNowTimeStamp()

Description

get Now Time Stamp

License

Open Source License

Declaration

public static long getNowTimeStamp() 

Method Source Code


//package com.java2s;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static long getNowTimeStamp() {
        long returnTimeStamp = 0;
        Date aDate = null;/*  w  ww . jav a2  s .  co  m*/
        try {
            aDate = convertStringToDate("yyyy-MM-dd HH:mm:ss", getNowDateTime());
        } catch (ParseException pe) {
            aDate = null;
        }
        if (aDate == null) {
            returnTimeStamp = 0;
        } else {
            returnTimeStamp = aDate.getTime();
        }
        return returnTimeStamp;
    }

    public static Timestamp getNowTimestamp() {
        String stampStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(System.currentTimeMillis());
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Timestamp ts = null;
        try {
            ts = new Timestamp(format.parse(stampStr).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ts;
    }

    /**
     * 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 {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static final String getNowDateTime(String strScheme) {
        String strReturn = null;
        Date now = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(strScheme);
            strReturn = sdf.format(now);
        } catch (Exception e) {
            strReturn = "";
        }
        return strReturn;
    }

    public static final String getNowDateTime() {
        String strReturn = null;
        Date now = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            strReturn = sdf.format(now);
        } catch (Exception e) {
            strReturn = "";
        }
        return strReturn;
    }

    public static String parse(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        String dateStr = sdf.format(d);
        return dateStr;
    }
}

Related

  1. getNanoString(Timestamp timestamp, boolean full, char nanoSep)
  2. getNbHours(Timestamp dateA, Timestamp dateB)
  3. getNextDayStart(java.sql.Timestamp stamp)
  4. getNextTimestamp(Timestamp previous, long nrDays)
  5. getNotNullTimestampValue(String timestamp, String format)
  6. getNowTimeStamp()
  7. getNowTimestamp()
  8. getOffsetDaysTime(Timestamp sysDate, int offsetDays)
  9. getPerformanceTime(Timestamp startTime)