Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

Converting Time as a string  to a Long

Thread view: 
Al Wilkerson - 24 Sep 2004 05:22 GMT
Hello,

Does anyone now how to access the setTimeInMillis method of the Calendar
Class. I have a time in the format of HH:MM:SS: which is a string, but I
want to
convert it to a long, so I can compare the times to figure out of two times
how far greater or lesser one is.

Thanks,

Signature

Al

Paul Lutus - 24 Sep 2004 06:48 GMT
> Hello,
>
> Does anyone now how to access the setTimeInMillis method of the Calendar
> Class.

Define "access". I generally get it this way:

long millis = new Calendar().getTimeInMillis();

Or:

long millis = System.currentTimeMillis();

Same result.

> I have a time in the format of HH:MM:SS: which is a string, but I
> want to
> convert it to a long, so I can compare the times to figure out of two
> times how far greater or lesser one is.

You only need time, yes? Not dates? And expressed in hours, minutes, and
seconds? Then you do not need a Calendar object, and you do not need
milliseconds. The simple way is to split the time string into sections and
multiply them together:

public class Test {
 
  static int timeSecondsFromString(String s)
  {
     int t = 0;
     String[] array = s.split(":");
     if(array.length == 3) {
        t = Integer.parseInt(array[0]) * 1440
            + Integer.parseInt(array[1]) * 60
            + Integer.parseInt(array[2]);
     }
     return t;
  }
 
  public static void main(String[]args)
  {
     if (args != null && args.length > 0) {
        int timeSeconds = timeSecondsFromString(args[0]);
        System.out.println(timeSeconds);
     }
  }
};

Input: 12:34:56

Output: 19376 seconds.

I will leave the time comparisons to you.

Signature

Paul Lutus
http://www.arachnoid.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.