View Single Post
  #1 (permalink)  
Old 05-14-07, 08: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

Reply With Quote