Java code for getting current time

sushmita picture sushmita · May 7, 2009 · Viewed 625.7k times · Source

I am searching code in java for fetching or synchronizing my local PC system time into my application.

Answer

Artur picture Artur · May 7, 2009

Try this:

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class currentTime {

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        System.out.println( sdf.format(cal.getTime()) );
    }

}

You can format SimpleDateFormat in the way you like. For any additional information you can look in java api:

SimpleDateFormat

Calendar