Current location: Hot Scripts Forums » Programming Languages » Everything Java » converting date-string to Date object


converting date-string to Date object

Reply
  #1 (permalink)  
Old 05-14-07, 09:56 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
converting date-string to Date object

I'm trying to convert a date-string, like "14-05-2007", to a Date object using the SimpleDateFormat class and the SimpleDateFormat.parse method.

There are a few ways the date-string can be formated, and i need to get the correct correspondig date. This is the code i'm using atm:
Java Code:
  1. package com.upjlib.audio.tags.utils;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.GregorianCalendar;
  7.  
  8. import com.upjlib.audio.tags.InvalidTagException;
  9.  
  10. public class DateFormat {
  11.  
  12.     public GregorianCalendar date = null;
  13.  
  14.     public DateFormat() {
  15.  
  16.     }
  17.  
  18.     public DateFormat(String format) throws InvalidTagException {
  19.         Date date = null;
  20.         if (date == null) {
  21.             try {
  22.                 date = tryFormat(format, "yyyy-MM-dd'T'HH:mm:ss");
  23.             } catch (ParseException e) {
  24.             }
  25.         }
  26.         if (date == null) {
  27.             try {
  28.                 date = tryFormat(format, "yyyy-MM-dd'T'HH:mm");
  29.             } catch (ParseException e) {
  30.             }
  31.         }
  32.         if (date == null) {
  33.             try {
  34.                 date = tryFormat(format, "yyyy-MM-dd'T'HH");
  35.             } catch (ParseException e) {
  36.             }
  37.         }
  38.         if (date == null) {
  39.             try {
  40.                 date = tryFormat(format, "yyyy-MM-dd");
  41.             } catch (ParseException e) {
  42.             }
  43.         }
  44.         if (date == null) {
  45.             try {
  46.                 date = tryFormat(format, "yyyy-MM");
  47.             } catch (ParseException e) {
  48.             }
  49.         }
  50.         if (date == null) {
  51.             try {
  52.                 date = tryFormat(format, "yyyy");
  53.             } catch (ParseException e) {
  54.             }
  55.         }
  56.  
  57.         if (date == null) {
  58.             throw new InvalidTagException("Invalid date format found: "
  59.                     + format);
  60.         } else {
  61.             this.date = new GregorianCalendar();
  62.             this.date.setTime(date);
  63.         }
  64.     }
  65.  
  66.     private Date tryFormat(String format, String pattern) throws ParseException {
  67.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  68.         return sdf.parse(format);
  69.     }
  70.  
  71.     public GregorianCalendar getDate() {
  72.         return date;
  73.     }
  74.  
  75.     public static void main(String[] args) {
  76.         try {
  77.             DateFormat df = new DateFormat("12/");
  78.             System.out.println(df.getDate().getTime());
  79.         } catch (Exception e) {
  80.             System.out.println(e.getMessage());
  81.         }
  82.     }
  83. }
When running this script, with an invalid date-string "12/", it still parses it as a valid date-string instead of throwing an InvalidTagException or ParseException. I found out that the last date-pattern ("yyyy") matches anything that is passed on to the SimpleDateFormat.parse method as long as it's not an empty String.

So now my question is: is there a way to get an exact match of the entered date-string and the date-pattern? It says in the manual that the SimpleDateFormat.parse method doesn't always use all characters of the entered date-string, so....

Thanks in advance
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-15-07, 09:08 AM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not sure I understand what you're doing with the DateFormat constructor. Wouldn't it be easier and more efficient to do it this way (plus it seems to parse correctly):

Code:
import java.text.*;
import java.util.*;

public class DateFormat 
{

    private Date d;
    public DateFormat(String format, String parseDate)
   {
        try
        {
           SimpleDateFormat sdf = new SimpleDateFormat(format);
           d = sdf.parse(parseDate);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
            d = null;
        }
    }

    public Date getDate() 
    {
        return d;
    }

    public static void main(String[] args) 
    {
        try 
        {
            DateFormat df = new DateFormat("yyyy-MM-dd'T'HH:mm:ss", "12/");
            System.out.println(df.getDate().getTime());
        } 
         catch (Exception e) 
         {
            System.out.println(e.getMessage());
        }
    }
}
You provide the format in the constructor, is that a problem?
__________________
my site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-15-07, 09:08 AM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
Of course, you couldn't shorten it even more by just creating a date through simpledateformat directly.
__________________
my site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-15-07, 10:06 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
The thing i want to do is get a Date object from a date-string. I get the date-string from a file, and there are different date-formats (yyyy, yyyy-MM, yyyy-MM-dd, etc.). So the problem is that the string i read from the file can be any of these formats. That's why i try to match the date-string against a certain format.

So if i enter a date-string like this one: "2007-05-15T15:57:23", it would return a Date object with 2007 as year, 05 as month, 15 as day, 15 as hour, 57 as minutes and 23 as seconds. But if i enter "2007-05-15" it would only return a Date object with 2007 as year, 05 as month and 15 as day.

Atm, when i enter "12/" which is an invalid date-pattern, it still matches the "yyyy" pattern, so that's basically my problem.

If you know an easier way to accomplish this, do let me know. I could use regex though.

I tried using your code, exactly as you posted it, but it didn't seem to work. it prints:
Quote:
java.text.ParseException: Unparseable date: "12/"
Thanx for looking into it
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-15-07, 07:20 PM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by UnrealEd View Post
The thing i want to do is get a Date object from a date-string. I get the date-string from a file, and there are different date-formats (yyyy, yyyy-MM, yyyy-MM-dd, etc.). So the problem is that the string i read from the file can be any of these formats. That's why i try to match the date-string against a certain format.

So if i enter a date-string like this one: "2007-05-15T15:57:23", it would return a Date object with 2007 as year, 05 as month, 15 as day, 15 as hour, 57 as minutes and 23 as seconds. But if i enter "2007-05-15" it would only return a Date object with 2007 as year, 05 as month and 15 as day.

Atm, when i enter "12/" which is an invalid date-pattern, it still matches the "yyyy" pattern, so that's basically my problem.

If you know an easier way to accomplish this, do let me know. I could use regex though.

I tried using your code, exactly as you posted it, but it didn't seem to work. it prints:


Thanx for looking into it
I thought you wanted a parse exception to occur when 12/ is entered? I'll take a look at matching the strings exactly a little latter tomorrow....
__________________
my site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BankAccount object oreinted help psi3000 Everything Java 6 03-16-06 11:07 AM
Return String values from a object saved in an array mr_wazzup Everything Java 1 04-15-05 10:11 AM
Urgent converting String to Date yymae Everything Java 1 01-16-04 12:22 PM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM


All times are GMT -5. The time now is 01:33 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.