Here you can find the source of toTimestamp2(long value, int nanos, int meta)
public static String toTimestamp2(long value, int nanos, int meta)
//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; } }