I am searching code in java for fetching or synchronizing my local PC system time into my application.
I am searching code in java for fetching or synchronizing my local PC system time into my application.
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:
Both
new java.util.Date()
and
System.currentTimeMillis()
will give you current system time.
System.currentTimeMillis()
everything else works off that.. eg new Date() calls System.currentTimeMillis().
Like said above you can use
Date d = new Date();
or use
Calendar.getInstance();
or if you want it in millis
System.currentTimeMillis()
You can use new Date () and it'll give you current time.
If you need to represent it in some format (and usually you need to do it) then use formatters.
DateFormat df = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM, new Locale ("en", "EN"));
String formattedDate = df.format (new Date ());
To get system time use Calendar.getInstance().getTime()
And you should get the new instance of Calendar each time to have current time.
To change system time from java code you can use a command line