Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: DateFormat and SimpleDateFormat

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DateFormat and SimpleDateFormat

    Hi All -

    Been struggling for this for the last 4 days or so...hoping someone can help.
    I have a file that I am parsing dates from in the below format:

    2012/58:20:36:41
    2012/54:13:34:56
    2012/54:15:08:16

    I move these into an array (data) as a string. I want to take the string --> change it into a date object --> then print it. Clearly I am struggling with my understanding of what SimpleDateFormat does....when printing I want to maintain the same format that I have. Any and all help is appreciated. Thanks

    for (int i = 0; i<data.size(); i++){
     
                DateFormat eachDate = new  SimpleDateFormat ("yyyy/DDD:HH:M:ss");
     
     
                try {
     
                    Date d1 = eachDate.parse(data.get(i));
                    System.out.println(d1);
     
                }    
     
                catch(ParseException pe) {
                    System.out.println("ERROR: could not parse date in string");                        
     
                }
            }


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: DateFormat and SimpleDateFormat

    If you define the data as an array, data.get(i) should be an object, not a string.Then eachDate.parse() need a string as parameter.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: DateFormat and SimpleDateFormat

    please tell how and what are you passing to this method !!
    regards

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: DateFormat and SimpleDateFormat

    Hi arvindbis,

    I'm passing string variables that are stored in the Array data.

    They are in this format:

    2012/58:20:36:41
    2012/54:13:34:56
    2012/54:15:08:16

    - Jason

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: DateFormat and SimpleDateFormat

    when printing I want to maintain the same format that I have.
    You are printing the Date object, which uses the Date.toString() method. If you want to print a Date object in the same format as the string passed to the parse method, then use the DateFormat.format() method.

Similar Threads

  1. SimpleDateFormat
    By smrtalex in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 7th, 2010, 09:44 PM
  2. DateFormat is not thread safe
    By trueacumen in forum Threads
    Replies: 5
    Last Post: August 15th, 2009, 02:16 AM

Tags for this Thread