Java Timestamp Format formatTimestampToLong(final String timestampStr)

Here you can find the source of formatTimestampToLong(final String timestampStr)

Description

format Timestamp To Long

License

Apache License

Declaration

public static long formatTimestampToLong(final String timestampStr) 

Method Source Code


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

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

import java.util.Calendar;
import java.util.Date;

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final Map<String, ThreadLocal<SimpleDateFormat>> timestampFormatPool = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
    private static final Object timestampFormatLock = new Object();
    private static String timestampPattern = "yyyy-MM-dd HH:mm:ss";

    public static long formatTimestampToLong(final String timestampStr) {
        Date date;/*from w  w  w .  j  a v  a  2  s . c  o m*/
        try {
            //         date = timestampFormat.parse(timestampStr);
            date = getTimestampFormat().parse(timestampStr);
        } catch (final ParseException e) {
            return 0L;
        }
        return date.getTime();
    }

    private static SimpleDateFormat getTimestampFormat() {
        ThreadLocal<SimpleDateFormat> tl = timestampFormatPool.get(timestampPattern);
        if (null == tl) {
            synchronized (timestampFormatLock) {
                tl = timestampFormatPool.get(timestampPattern);
                if (null == tl) {
                    tl = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected synchronized SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(timestampPattern);
                        }
                    };
                    timestampFormatPool.put(timestampPattern, tl);
                }
            }
        }
        return tl.get();
    }

    public static Date getTime(final int day) {
        return getTime(new Date(), day);
    }

    public static Date getTime(final Date date, final int day) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
        return calendar.getTime();
    }
}

Related

  1. formatTimestamp(Timestamp timestamp, String pattern)
  2. formatTimestamp(Timestamp ts, int dateStyle, int timeStyle, Locale locale)
  3. formatTimeStampMsec(long epochTime)
  4. formatTimestampTime(Timestamp ts, boolean colon)
  5. formatTimestampToIndex(java.sql.Timestamp timestamp)
  6. formatTimestampWithSlashes( java.sql.Timestamp tsZeitpunkt)
  7. formatTimeToString(long timestamp)
  8. formatToLong(String format)
  9. formatToString(long timestamp, String datePattern)