Results 1 to 10 of 10
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    135
    Rep Power
    0

    Default Date from string

    Hi
    I have a date time string like this:
    Java Code:
    String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
    I would like to convert this to a date like this: 2018-04-10
    How can I do this?
    I have tried this:
    Java Code:
    Date result;
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    result = formatter.parse (datetimeString);
    But I get "Unparseable date"

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Date from string

    I get "Unparseable date"
    The String you are trying to parse: "Tue Apr 10 15:19:06 CEST 2018"
    does not match the format specified in the SimpleDateFormat class's constructor: "yyyy-MM-dd"

    The input String has
    Tue - day
    Apr - month
    10 - day
    15:19:06 time
    CEST zone
    2018 year

    The parse String needs to match that.

    Look at the API doc for the SimpleDateFormat class and build a new format String that matches the String you are trying to parse.
    Last edited by Norm; 04-12-2018 at 04:31 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,170
    Rep Power
    12

    Default Re: Date from string

    You should also be using DateTimeFormatter and LocalDateTime classes.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    135
    Rep Power
    0

    Default Re: Date from string

    Thanks. I will have a look.
    The plan is to get the date to be in the format yyyy-MM-dd

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Date from string

    get the date to be in the format yyyy-MM-dd
    If the input is a String representing a date,
    and the desired output is another String with that date shown in a different way,
    one way to do the conversion is to parse the first String into some date object
    and then format that date object into the desired String.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    135
    Rep Power
    0

    Default Re: Date from string

    Hm.. I have tried this (from reading the api):

    Java Code:
    String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
    Date result;
    SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
    result = formatter.parse (datetimeString);
    However, same result...
    Last edited by asai; 04-12-2018 at 08:53 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Date from string

    Try the parsing one item at a time to find the problem by commenting out the trailing input and format Strings. Keep adding in items one at a time until the parse fails. They check that one item.
    Start here:
    Java Code:
          String datetimeString = "Tue"; // Apr 10 15:19:06 CEST 2018";
          SimpleDateFormat formatter = new SimpleDateFormat("EEE"); // MMM d HH:mm:ss z yyyy");
          Date result = formatter.parse (datetimeString);
          System.out.println(result);   // Tue Jan 06 00:00:00 EST 1970
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    384
    Rep Power
    4

    Default Re: Date from string

    On a side note: If the whole date parsing drama becomes to tideous and the date String you receive always has the same format you can use the old Tokenizer trick: take apart the String and reuse/parse what you need.
    Java Code:
    String[] myTokens = dateTimeString.split(“\\s”);
    MyTokens[1] will be your month (String) now, myTokens[5] will be the year (String).

  9. #9
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    135
    Rep Power
    0

    Default Re: Date from string

    Quote Originally Posted by benji2505 View Post
    On a side note: If the whole date parsing drama becomes to tideous and the date String you receive always has the same format you can use the old Tokenizer trick: take apart the String and reuse/parse what you need.
    Java Code:
    String[] myTokens = dateTimeString.split(“\\s”);
    MyTokens[1] will be your month (String) now, myTokens[5] will be the year (String).
    I tried the parsing one at the time, and the problem was with the day (EEE).
    Ended up using the string split method. :)
    Works fine.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,170
    Rep Power
    12

    Default Re: Date from string

    Well since you already applied the brute force method, here's how to use the API.

    Java Code:
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    
    public class DateTest {
    
    
       public static void main(String[] args) throws Exception {
          String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
    
          String from_format = "E MMM dd HH:mm:ss z yyyy";
          String to_format = "yyyy-MM-dd";
          
          DateTimeFormatter from_formatter = DateTimeFormatter.ofPattern(from_format);
          DateTimeFormatter to_formatter = DateTimeFormatter.ofPattern(to_format);
          
          LocalDateTime ldt = LocalDateTime.parse(datetimeString, from_formatter);
          
          System.out.println(ldt.format(to_formatter));
       }
    }
    In the DateTimeFormatter class it is important to understand the format symbol AND the
    presentation descriptions for proper symbol count.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. string date to formatted date string
    By reach2sudhakar in forum New To Java
    Replies: 4
    Last Post: 06-20-2011, 10:06 AM
  2. Convert String to Date?
    By bochra in forum New To Java
    Replies: 4
    Last Post: 11-15-2010, 10:41 AM
  3. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  4. convert String date to Date
    By computerbum in forum New To Java
    Replies: 7
    Last Post: 09-18-2010, 03:26 PM
  5. Replies: 7
    Last Post: 11-03-2009, 02:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •