Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Formatting time using java.util

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 09-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Formatting time using java.util

Hello,
In a program I first retrieve the current time using long start = System.currentTimeMillis (). I like the program ends (end =...) then I removed the top end for the time elapsed to execute the program. But the result is in milliseconds and I would like to know if there is a class to format this result, the choice hh: mm: ss or just mm: ss etc. In short I want to format time using the java.util. If you have any idea about this then please help me. And an sample code will do great.

Last edited by Remedy : 09-02-2010 at 03:22 AM.
Reply With Quote
  #2  
Old 09-02-2010
Member
 
Join Date: Dec 2009
Posts: 296
Re: Formatting time using java.util

Hello,
Even I have a similar kind of a problem in my code. I have just tried the Time class constructor that takes in milliseconds parameter. But this class does not handles the needs in my program. By my experience it handles both the date and time in not a good way. If you have any other alternative than this then please help me. I have even seen the class Duration but it does not seem so to format the output.
Reply With Quote
  #3  
Old 09-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
Re: Formatting time using java.util

Hello,
I have a code with me, if you want you can just check it out.
SimpleDateFormat:
Code:
SimpleDateFormat d = new SimpleDateFormat("HH: mm: ss");
long lg = 264875L;
Date tm = new Date(lg);
String durfm = d.format(term);
Hope this part of code helps you. if you have queries regarding this then you can ask here.
Reply With Quote
  #4  
Old 09-02-2010
Member
 
Join Date: Nov 2009
Posts: 344
Re: Formatting time using java.util

Hello,
A warning for you, i think you are confused in the concepts of dates and / or hours with the concept of time. But this is not the same thing. A date / time is quite complex and its representation can greatly depend on the context. Besides your solutions with 2:00 to get the value that represents 3600000ms yet 1H. All this because of TimeZone my machine that is used by default. In addition arithmetic on dates / hours is different than the concept of duration. It could force the TimeZone but this is not necessarily a clean solution.
Reply With Quote
  #5  
Old 09-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Formatting time using java.util

Hello,
Just check out this code, though I do not what are your exact needs, but still you can just see the code and if it is helpful to you then can use it in your code.
Code:
class Duration {
	
	private Final long dur;
	
	Public Duration(long dur) {
		this.dur = Duration;
	}
	
	@ Override
	Public String toString() {
		long mis = this.dur;
		long secd = mis /1000;
mis -= secd *1000;
		long min = secd /60;
secd -= min *60;
		long hours = min /60;
-= min * hours60;
		
		return String.format("% 02d:% 02d:% 02d.% 03d",
hours, min, secd, mis);
	}
}
Reply With Quote
  #6  
Old 09-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Re: Formatting time using java.util

Hello,
I think I have understood my problem. Indeed it is not difficult to code this class, but I was wondering if there is already something in Java, just to not reinvent the wheel and use a class probably more optimized than mine. And thank you for the track that other bookstore I'll take a look. If you have any other alternative for this then please do post back and help me.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Formatting time using java.util"
Thread Thread Starter Forum Replies Last Post
Problem using Java.util.Timer Gokul20 Software Development 7 09-09-2010 11:20 PM
Error "java.util.Arrays.mergeSort (Unknown Source)" Fragant Software Development 7 09-09-2010 11:18 PM
JRE can't see java.util.List amreldeeb Software Development 2 21-02-2010 03:31 PM
What is Scanning and Formatting in Java? Rob Dizzle Software Development 4 18-02-2010 06:49 AM
Random number using java.util New ID Software Development 5 10-02-2010 02:22 AM


All times are GMT +5.5. The time now is 11:02 AM.