Help coderanch get a
new server
by contributing to the fundraiser
  • 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

doubt in parsing

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can someone please clarify my doubt with K&B book Chapter 6 written below:

The last method you should be familiar with is the parse() method. The
parse() method takes a String formatted in the style of the DateFormat instance
being used, and converts the String into a Date object. As you might imagine, this is
a risky operation because the parse() method could easily receive a badly formatted
String. Because of this, parse() can throw a ParseException.

The parsing code is below :

import java.util.*;
import java.text.*;
class Dates3
{ public static void main(String[] args)
{



Date d1 = new Date();
System.out.println("d1 = " + d1.toString());
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
;
System.out.println(df.format(d1));
try {
Date d2 = df.parse(s);
System.out.println("parsed = " + d2.toString());
} catch (ParseException pe) {
System.out.println("parse exc"); }

}
}

Now my doubt is how the ParseException will thrown? The parameter passed to the parse() method in the example is a String which is parsed/converted into format of a Date object.Can someone give some example in which a ParseException is thrown with the help of an example if possible...In a nut shell,can someone give an example of badly formatted String which cannot be converted to a date and a ParseException is thrown?

Thanks in advance
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any string that does not match the expected format will cause a ParseException to be thrown. Here's an example:

Please use code tags when you post Java code.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies about the incorrect code. Below is the example:

import java.util.*;
import java.text.*;
class Dates3
{ public static void main(String[] args)
{
Date d1 = new Date(1000000000000L);
System.out.println("d1 = " + d1.toString());
DateFormat df = DateFormat.getDateInstance(
DateFormat.SHORT);
String s = df.format(d1);
System.out.println(s);
try {
Date d2 = df.parse(s);
System.out.println("parsed = " + d2.toString());
} catch (ParseException pe) {
System.out.println("parse exc"); }

}
}
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jesper...It was silly of me not to understand that in the first place.Yes,I will use the code tags while posting the code henceforth..

Cheers,
Moieen
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run the below code :


The O/P is :

d1 = Sun Sep 09 07:16:40 GMT+05:30 2001
September 9, 2001
parsed = Sun Sep 09 00:00:00 GMT+05:30 2001

I don't understand how the bold text time appears in the parsed O/P... Does truncation happen while parsing in this example?

Thanks
 
author
Posts: 23956
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

I don't understand how the bold text time appears in the parsed O/P... Does truncation happen while parsing in this example?



Take a look at the string that you sent to the parse() method....



Given this as the input, what do you expect the parse() method to return in terms of hours, minutes, and seconds?

Henry
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am inputting just the date represented as String w/o timings as displayed on the second line as a parameter to the parse() method of the df so I guess the time portion is lost in the 3rd line of the O/P and its reset to 0000 hours.

Am I on the right track?

Thanks
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the time portion is already lost when you are formatting the string-value, but: yes, you are on the right way
 
Did you just should on me? You should read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic