get Time Stamp in yyyyMMddhhmmssSSS format - Java java.sql

Java examples for java.sql:Timestamp

Introduction

The following code shows how to get Time Stamp.

Demo Code

//package com.java2s;

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

public class Main {
    public static void main(String[] argv) {
        System.out.println(getTimeStamp());
    }/*from ww w  .  j a  v  a  2  s.com*/

    public static String getTimeStamp() {

        String rtnStr = null;

        String pattern = "yyyyMMddhhmmssSSS";

        try {
            SimpleDateFormat sdfCurrent = new SimpleDateFormat(pattern,
                    Locale.KOREA);
            Timestamp ts = new Timestamp(System.currentTimeMillis());

            rtnStr = sdfCurrent.format(ts.getTime());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return rtnStr;
    }
}

Related Tutorials