• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unparseable date

 
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
please help me on how to read two different time from keyboard and get the time difference in hours and minutes only.

here is my code


the output is
java.text.ParseException: Unparseable date: ""
at java.text.DateFormat.parse(DateFormat.java:366)



 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What Scanner#next does is to read from where you are until the next whitespace. So you enter
05/29/2015 15:23
and the Scanner returns
05/29/2015
Now that won't work for the simple date format parser. Try nextLine instead of next.

I shall go back to your post and add code tags which will make it look much better as long as you have indented the code. Alwys use the code tags.
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but even then it does not work i just want to read time alone
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james kinyua wrote:
the output is
java.text.ParseException: Unparseable date: ""
at java.text.DateFormat.parse(DateFormat.java:366)



The error message is saying that it is trying to parse a blank string. So, this is not an issue of parsing incorrectly. This is an issue of getting the wrong input. Most likely, you type an extra return (or had an extra return from previous input).

Regardless, your best action to resolve this is probably to read again until you get an actual string that you can parse. And even so, what if the user enters an invalid string? Are applications supposed to be breakable by user input?

Henry
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How on earth can you get an empty String from that Scanner with next(), unless you change its delimiter? You can get an empty String with nextLine() so I was about to ask OP what his book says nextLine() does, then I saw there is no sign of nextLine() being used.

I tried it myself changing next() to nextLine() twice and get a difference in time printed.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How on earth can you get an empty String from that Scanner with next(), unless you change its delimiter?



Agreed, There are definitely portions of code that the OP is not showing us.

Henry
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my full code.
is it possible to parse time alone without date?

package vehicleturnabout;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Vehicleturnabout {

public static void main(String[] args) {
String t1,t2;

[code = java]
//HH converts hour in 24 hours format (0-23)
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm");

Scanner input=new Scanner(System.in);

System.out.println("Time out");
t1=input.nextLine();
System.out.println("Time in");
t2=input.nextLine();
try {
Date d1 = format.parse(t1);
Date d2= format.parse(t2);

//in milliseconds
long diff = d2.getTime() - d1.getTime();

//long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;

System.out.print("FIELD TIME IS");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");

} catch (Exception e) {
e.printStackTrace();
}
[/code]

}

}


 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. i just removed "MM/dd/yyyy" in the SimpleDateFormat format = new SimpleDateFormat("HH:mm")
and it worked
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic