Skip to content
Search

Java.text.SimpleDateFormat

lastmod 2020-01-21created 2020-01-142 edits

Among the functions of Java's System class, the method that returns the current time is as follows.

public static native long currentTimeMillis();

The return type is long; it returns the value in units of 1/1000 second counted from January 1, 1970.

Simple usage

public static final String getTimeStamp() {
    Calendar calender = Calendar.getInstance();
    calender.setTimeInMillis(System.currentTimeMillis());
    SimpleDateFormat format = new SimpleDateFormat("yyMMdd-HHmmssSSS", Locale.KOREA);
    return format.format(calender.getTime());
}

Getting the current date and time

long now = System.currentTimeMillis();
SimpleDateFormat sdfNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strNow = sdfNow.format(new Date(now));

How to convert a Timestamp to yyyy-mm-dd hh:mm:ss format?

SimpleDateFormat sdfCurrent = new SimpleDateFormat ("yyyy-mm-dd hh:mm:ss"); 
Timestamp currentTime = new Timestamp(Long.parseLong("1141952708997")); 
String today = sdfCurrent.format(currentTime); 
System.out.println("today==="+today);

See also

Favorite site

author Zer0 sitemap