i am searching code in java for fetching or synchronising my local PC system time into my application
Not really sure about what you meant, but you probably just need
Date d = new Date();
Both
new java.util.Date()
and
System.currentTimeMillis()
will give you current system time.
Like said above you can use
Date d = new Date();
or use
Calendar.getInstance();
or if you want it in millis
System.currentTimeMillis()
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
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 ());
System.currentTimeMillis()
everything else works off that.. eg new Date() calls System.currentTimeMillis().
Try this:
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class currentTime {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.getTime();
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: