Java Timestamp Create toTimestamp2(long value, int nanos, int meta)

Here you can find the source of toTimestamp2(long value, int nanos, int meta)

Description

to Timestamp

License

Open Source License

Declaration

public static String toTimestamp2(long value, int nanos, int meta) 

Method Source Code

//package com.java2s;
/**/*from  w  w w  .j a  v a2 s  .c  o m*/
 * Project: ${puma-server.aid}
 * 
 * File Created at 2012-6-11 $Id$
 * 
 * Copyright 2010 dianping.com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of Dianping
 * Company. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with dianping.com.
 */

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public class Main {
    public static String toTimestamp2(long value, int nanos, int meta) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp time = new java.sql.Timestamp(value * 1000L);
        String strValue = sdf.format(time);
        return strValue + microSecondToStr(nanos, meta);
    }

    private static String microSecondToStr(int nanos, int meta) {
        if (meta > 6) {
            throw new IllegalArgumentException("unknow useconds meta : " + meta);
        }
        String microSecond = "";
        if (meta > 0) {
            microSecond = String.valueOf(nanos);
            if (microSecond.length() < meta) {
                int total = meta % 2 == 0 ? meta : meta + 1;
                int len = total - microSecond.length();
                StringBuilder prefixMicroSecond = new StringBuilder(len);
                for (; len > 0; len--) {
                    prefixMicroSecond.append("0");
                }
                microSecond = prefixMicroSecond.toString() + microSecond;
            }
            microSecond = microSecond.substring(0, meta);
            return "." + microSecond;
        }
        return microSecond;

    }
}

Related

  1. toTimestamp(String str, String format)
  2. toTimestamp(String value)
  3. toTimestamp(String yyyymmddhhmmss)
  4. toTimestamp2(long seconds, int fraction, int width)
  5. toTimestamp2(long seconds, int nanos)
  6. toTimestampByHour(Date date, int hour)
  7. toTimestampFromGMT(int yy, int mm, int dd, int hh, int mi, int ss)
  8. toTimestampFromLong(long millis)
  9. toTimestampFromTime(String time)