Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2010
    Posts
    22
    Rep Power
    0

    Default string date to formatted date string

    HI,
    Is there a dateformat for this requirement?

    String date = 201010 - 2010-10
    String date = 20101010 - 2010-10 (only year and month to be displayed with a - in between).

    tried different ways getting Unparseable date: "201011", but i will get the date like this only.

    Now I am doing by substring that date, concatenate it with - in between with the substringed values (year (0,4), month (4,6) concat (year, -, month)).

    which looks to be odd.

    Thanks.
    Last edited by reach2sudhakar; 06-16-2011 at 12:42 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Show us the code, because you should be able to parse yyyyMM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    If this is all about formatting a String, and has no relation to the Date value:
    Java Code:
    String input = "20101010";
    String output = input.replaceAll("(\\d{4})(\\d{2})\\d*", "$1-$2");
    Tweak as necessary if all your inputs aren't as simple as the two you showed here.

    db

  4. #4
    Join Date
    Sep 2010
    Posts
    22
    Rep Power
    0

    Default

    Hi,
    I have coded like this:

    public static String getDateString(String date) throws ParseException {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dateFormat.parse(date));
    return dateFormat.format(calendar.getTime());
    }

    Getting error like below:

    Exception in thread "Main Thread" java.text.ParseException: Unparseable date: "201010"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at util.Utility.getDateString(Utility.java:111)
    at util.Utility.main(Utility.java:35)


    Please let me know what went wrong here. I will be getting input date either as 201010 or 20101010 requirement is to convert it to 2010-10 (only year and month).

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Quote Originally Posted by reach2sudhakar View Post
    Getting error like below:
    It should be obvious to you that the String "201010" doesn't match the format pattern "yyyy-MM"

    Once again, why are you complicating this with String/Date/String conversions, when all you want to do is String manipulation?

    db

Similar Threads

  1. String to date conversion
    By nn12 in forum New To Java
    Replies: 4
    Last Post: 11-04-2010, 07:59 AM
  2. 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
  3. convert String date to Date
    By computerbum in forum New To Java
    Replies: 7
    Last Post: 09-18-2010, 03:26 PM
  4. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  5. String to Date conversion
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 07:16 PM

Posting Permissions

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