• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Geting Current Time in Seconds?

 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to get the curren time of the system in seconds.Plz help!.
Bye,
Viki.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about System.currentTimeMillis() and a little mathematics (such as / 1000)?
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dirk,
I have tried the same before but got odd result.May be I am doing some silly mistake anywhere.Here is my code with o/p

O/P:1033139787
Bye,
Viki.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably think it's odd because it returns the number of milliseconds since the epoch. From the api:

the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are trying to get the seconds in the current minute of your system. e.g. If system time is 15:23:15, then you are trying to get 15. Is that what you're trying to do? If so, you need to check out the GregorianCalendar class
 
David Hewitt
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are two possible ways of getting the seconds in the current minute.
Use Calendar, eg:
Calendar calendar = Calendar.getInstance();
System.out.println("Seconds in current minute = " + calendar.get(Calendar.SECOND));
Or, use SimpleDateFormat:
SimpleDateFormat formatter = new SimpleDateFormat("ss");
System.out.println("Seconds in current minute = " + formatter.format(new Date()));
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic