Time Conversion

Hi,Can anybody help me converting time formats?.e.g. I have a time format 06:20 and i want to convert this format to 6hrs20min.Thanks & Regards,Antonio
[204 byte] By [AntonioRa] at [2007-11-27 8:30:41]
# 1
And your problem is?
sabre150a at 2007-7-12 20:25:49 > top of Java-index,Java Essentials,Java Programming...
# 2
> Hi,> > Can anybody help me converting time formats?I know what can help you. The SimpleDateFormat class.
cotton.ma at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 3
Hi,SimpleDateFormat works for dates but i don't have a date, i have a time only as 06:20 and i want this to be converted to 6hrs20min.Thanks & Ragards,Antonio
AntonioRa at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 4
> Hi,> > SimpleDateFormat works for dates BZZZZZZZZZZZZTSorry incorrect. Please try again.
cotton.ma at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 5
Did you consider String concatenation if the "time" you have is nothing but yet another semicolon-divided String?
CeciNEstPasUnProgrammeura at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 6
> Did you consider String concatenation if the "time"> you have is nothing but yet another semicolon-divided> String?That really doesn't seem a very good idea.
cotton.ma at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 7

> > Did you consider String concatenation if the

> "time"

> > you have is nothing but yet another

> semicolon-divided

> > String?

>

> That really doesn't seem a very good idea.

It does to me though I would do it with String.replaceAll().

sabre150a at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 8

> > > Did you consider String concatenation if the

> > "time"

> > > you have is nothing but yet another

> > semicolon-divided

> > > String?

> >

> > That really doesn't seem a very good idea.

>

> It does to me though I would do it with

> String.replaceAll().

Would one of you care to explain why that would be a better solution then SimpleDateFormat?

Because (a) you're reinventing the wheel with all that that implies (b) some validation is provided free of charge with SimpleDateFormat

cotton.ma at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 9

Anyway here are the codes.

import java.text.SimpleDateFormat;

public class Test{

public static void main(String[] args)throws Exception{

SimpleDateFormat in = new SimpleDateFormat("HH:mm");

SimpleDateFormat out = new SimpleDateFormat("H'hrs'mm'min'");

String[] tests = {"06:20","17:45","0:83"};

for(int i=0;i<tests.length;i++){

System.out.println(out.format(in.parse(tests[i])));

}

}

}

>

cotton.ma at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 10

> > > > Did you consider String concatenation if the

> > > "time"

> > > > you have is nothing but yet another

> > > semicolon-divided

> > > > String?

> > >

> > > That really doesn't seem a very good idea.

> >

> > It does to me though I would do it with

> > String.replaceAll().

>

> Would one of you care to explain why that would be a

> better solution then SimpleDateFormat?

>

> Because (a) you're reinventing the wheel with all

> that that implies (b) some validation is provided

> free of charge with SimpleDateFormat

How does SDF cope with 72:20 ?

sabre150a at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 11
> Would one of you care to explain why that would be a> better solution then SimpleDateFormat?The solution is the simplest that fits the current requirements, which is String manipulation.
CeciNEstPasUnProgrammeura at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 12

> > > > > Did you consider String concatenation if the

> > > > "time"

> > > > > you have is nothing but yet another

> > > > semicolon-divided

> > > > > String?

> > > >

> > > > That really doesn't seem a very good idea.

> > >

> > > It does to me though I would do it with

> > > String.replaceAll().

> >

> > Would one of you care to explain why that would be

> a

> > better solution then SimpleDateFormat?

> >

> > Because (a) you're reinventing the wheel with all

> > that that implies (b) some validation is provided

> > free of charge with SimpleDateFormat

>

> How does SDF cope with 72:20 ?

How would you like it to deal with it?

By default it rolls.

Or you can make it non lenient and have it blow up.

cotton.ma at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 13

> > Would one of you care to explain why that would be

> a

> > better solution then SimpleDateFormat?

>

> The solution is the simplest that fits the current

> requirements, which is String manipulation.

How is that easier again then what I posted?

One less line of code?

cotton.ma at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 14

> > Would one of you care to explain why that would be

> a

> > better solution then SimpleDateFormat?

>

> The solution is the simplest that fits the current

> requirements, which is String manipulation.

Agreed. SDF is proposed as the answer to ALL time and date formatting problems but it does not deal well with values outside the normal ranges (i.e. 63 minutes) and it distorts displays of time when dealing with a number of seconds or milliseconds that cross leap second and year boundaries.

sabre150a at 2007-7-12 20:25:50 > top of Java-index,Java Essentials,Java Programming...
# 15

> > > Would one of you care to explain why that would

> be

> > a

> > > better solution then SimpleDateFormat?

> >

> > The solution is the simplest that fits the current

> > requirements, which is String manipulation.

>

> Agreed. SDF is proposed as the answer to ALL time and

> date formatting problems but it does not deal well

> with values outside the normal ranges (i.e. 63

> minutes) and it distorts values of time when dealing

> with a number of seconds or milliseconds that cross

> leap second and year boundaries.

This is rubbish, SDF does no such thing. Calendar and date are doing that and while that may seem pedantic to you it seems a pretty important point to me.

We do not know what the requirements of the OP are really, and we are probably unlikely to know. There are several reasons, which I gave, as to why SDF is the solution to choose, in my opinion.

As to the points the two of you have made:

- the point about distortion is convoluted although maybe perhaps valid. but only valid if it violates the OP's requirements which we do not know

- the point about simple is rubbish

Regexes are not the answer to every problem in life or Java. Especially when a pre-existing, well tested, well understood later by other programmers looking at the code solution exists.

cotton.ma at 2007-7-21 22:41:41 > top of Java-index,Java Essentials,Java Programming...
# 16
> We do not know what the requirements of the OP are> really, Which is all I wanted to express. So far we can't even be sure that he's talking about Java.
CeciNEstPasUnProgrammeura at 2007-7-21 22:41:41 > top of Java-index,Java Essentials,Java Programming...
# 17

<snip>

> This is rubbish, SDF does no such thing. Calendar

> and date are doing that and while that may seem

> pedantic to you it seems a pretty important point to

> me.

Not rubbish! Not pedantic!

>

> We do not know what the requirements of the OP are

Agreed - which, unlike you, is why in my first post I did not propose a solution.

> really, and we are probably unlikely to know. There

> are several reasons, which I gave, as to why SDF is

> the solution to choose, in my opinion.

Your opinion.

>

> As to the points the two of you have made:

>

> - the point about distortion is convoluted although

> maybe perhaps valid. but only valid if it violates

> the OP's requirements which we do not know

> - the point about simple is rubbish

Not rubbish.

>

> Regexes are not the answer to every problem in life

> or Java.

Horses for courses. I proposed regex partly to offset the rush towards SDF though I could equally well have proposed a 'substring' approach.

> Especially when a pre-existing, well tested,

> well understood later by other programmers looking at

> the code solution exists.

I see more potential problems to the use of SDF than I do to using regex or substring.

sabre150a at 2007-7-21 22:41:41 > top of Java-index,Java Essentials,Java Programming...
# 18

> > As to the points the two of you have made:

> >

> > - the point about distortion is convoluted

> although

> > maybe perhaps valid. but only valid if it violates

> > the OP's requirements which we do not know

> > - the point about simple is rubbish

>

> Not rubbish.

Because why exactly?

> > Especially when a pre-existing, well tested,

> > well understood later by other programmers looking

> at

> > the code solution exists.

>

> I see more potential problems to the use of SDF than

> I do to using regex or substring.

That is primarily because you are biased towards regexes.

At any rate I am willing to concede about, as you phrased it, distortion, might be an issue in some cases. But the simplicty argument is rubbish, I'm sorry but it is.

cotton.ma at 2007-7-21 22:41:41 > top of Java-index,Java Essentials,Java Programming...
# 19

> > > As to the points the two of you have made:

> > >

> > > - the point about distortion is convoluted

> > although

> > > maybe perhaps valid. but only valid if it

> violates

> > > the OP's requirements which we do not know

> > > - the point about simple is rubbish

> >

> > Not rubbish.

>

> Because why exactly?

As a counter - why do you say it is rubbish? Exactly what aspect of my earlier statements do you think is rubbish?

>

> > > Especially when a pre-existing, well tested,

> > > well understood later by other programmers

> looking

> > at

> > > the code solution exists.

> >

> > I see more potential problems to the use of SDF

> than

> > I do to using regex or substring.

>

> That is primarily because you are biased towards

> regexes.

No really true - I am biased towards regex for problems involving string manipulation. You view the OPs problem as date conversion problem, I view it as a string manipulation.

>

> At any rate I am willing to concede about, as you

> phrased it, distortion, might be an issue in some

> cases. But the simplicty argument is rubbish, I'm

> sorry but it is.

I'm not sure I understand this? The simplicity argument is primarily yours. I have to go out now to help someone move house but I will re-read the 'simplicty' arguments later and get back.

sabre150a at 2007-7-21 22:41:41 > top of Java-index,Java Essentials,Java Programming...
# 20
Thanks!It Works..:-)Regards,Antonio
AntonioRa at 2007-7-21 22:41:41 > top of Java-index,Java Essentials,Java Programming...
# 21
'cotton.m ' - it would seem that your approach is just what the doctor ordered! :-(
sabre150a at 2007-7-21 22:41:41 > top of Java-index,Java Essentials,Java Programming...