Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with parseInt()....

 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
My requirement is that I have to take 2 user inputs-1 timestamp(say in the format yyyy/mm/dd-hh:min:sec) and a zone offset(for example -5:30 hrs) and add the offset to the timestamp to generate a new date.
Accepting the input and performing addition is ok(Calendar add mthd)...but the problem is am taking offset as a string and then using parseInt to convert.So long as i dont use signs,it works...but if my offset is something like -5:30,its throwing the number format exception...can somebody advise me how to proceed??

Thanks and regards,
Thejaswini.
[ November 10, 2005: Message edited by: thejaswini ramesh ]
 
Hemal Pandya
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by thejaswini ramesh:
Hello everybody,
My requirement is that I have to take 2 user inputs-1 timestamp(say in the format yyyy/mm/dd-hh:min:sec) and a zone offset(for example -5:30 hrs) and add the offset to the timestamp to generate a new date.
Accepting the input and performing addition is ok(Calendar add mthd)...but the problem is am taking offset as a string and then using parseInt to convert.So long as i dont use signs,it works...but if my offset is something like -5:30,its throwing the number format exception...can somebody advise me how to proceed??


offset= t.getText();//input in a textfield
System.out.println("the offset is :" + offset);
StringTokenizer st = new StringTokenizer(offset,":");

while(st.hasMoreTokens()) {
String h1 = st.nextToken();
System.out.println("h:" + h1);
hi = Integer.parseInt(h1);//doesn't accept -5:30
System.out.println("int hi:" + hi);

String m1 = st.nextToken();
System.out.println("m:" + m1);
mi = Integer.parseInt(m1);
System.out.println("int mi:" + mi);

Thanks and regards,
Thejaswini.


The String "-5" should can be processed by Integer.parseInt without exception. Can you post the output you get with the above?

But your comment says it doesn't accept "-5:30", which it ceetainly won't. Are you sure the code works when the offset is "5:30" and "8:00"?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ParseInt method wont accept 5:30 because it's not a Valid number.

FYI: If you want to convert a time to any specific Time zone you can use the class called "DateFormat" & "TimeZone"
[ November 10, 2005: Message edited by: Srinivasa Raghavan ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srinivasa Raghavan:
FYI: If you want to convert a time to any specific Time zone you can use the class called "DateFormat" & "TimeZone"


Thank you for your advice, but the question is somewhat different.
Suppose you have a string like "2005.11.10 07:48:36-0500". I see how
SimpleDateFormat can parse that to return a Date, but how can one use
(Simple)DateFormat to extract a TimeZone or offset?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but how can one use
(Simple)DateFormat to extract a TimeZone or offset?

I thought Ramesh needs to get the time in difference time zone hence i added my comments in (FYI). This can be done by dateFormatter.setTimeZone( timeZone obj)
 
Hemal Pandya
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by thejaswini ramesh:
Thanks Hemal and Srinivasa...now it seems negative offsets like -5:30 work for todays date but other inputs it doesn't!!I will paste my entire code with sample output...pls help me analyse.Also can you suggest me a more concise way of implementing this?

Thejaswini



That's too much code to debug. Try to write a small program that demonstrates the problem, e.g. by stripping all swing and input functionality and instead using hard-coded values.

Taking this route my help you solve the problem on your own as well.
 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for your valueable suggestions...hemal I did debug by hardcoding it...was able to solve tht...

This is accepting offset input with/without the sign...please see if this can be made more concise:

String offset = "+5:30";
System.out.println("the offset is :" + offset);


StringTokenizer st = new StringTokenizer(offset,":");

try
{
while(st.hasMoreTokens()) {
String h1 = st.nextToken();
System.out.println("h:" + h1);

StringTokenizer s = new StringTokenizer(h1,"+",false);
if(s.hasMoreTokens()) {
String hr = s.nextToken();
hi = Integer.parseInt(hr);
} else
hi =Integer.parseInt(h1);

System.out.println("int hi:" + hi);

String m1 = st.nextToken();
System.out.println("m:" + m1);
mi = Integer.parseInt(m1);
System.out.println("int mi:" + mi);

newCal.add(newCal.HOUR_OF_DAY,hi);
newCal.add(newCal.MINUTE,mi);

Date d2= newCal.getTime();
System.out.println("the modified date is :" + d2);

}
}catch(Exception e) {
System.out.println("exception occured");
}
 
Hemal Pandya
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by thejaswini ramesh:
Thanks everybody for your valueable suggestions...hemal I did debug by hardcoding it...was able to solve tht...

This is accepting offset input with/without the sign...please see if this can be made more concise:

String offset = "+5:30";
System.out.println("the offset is :" + offset);
StringTokenizer st = new StringTokenizer(offset,":");
[snip]
}


See String.split(), try offset.split(":");
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic