-3

I am getting date in form of "20150119" and I want to convert it into the format like: "19. January 2015" .

How can I convert date in such format.

I tried below code:

private void convertDate() {
        String m_date = "20150119";
        SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy.MM.dd");
        try {
            Date date = originalFormat.parse(m_date.toString());
            Log.e("Date is====", date.toLocaleString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

But its giving me error:

java.text.ParseException: Unparseable date: "20150119" (at offset 8)

Jonny C
  • 1,943
  • 3
  • 20
  • 36
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • SimpleDateFormat? `yyyyMMdd` to parse the date? What did you try to format ? – Blackbelt May 11 '15 at 12:35
  • 1
    You getting a date in form of "20150119" but Your question is about an integer....is it really an integer or is it a string? – Opiatefuchs May 11 '15 at 12:38
  • 1
    delete the points in Your simpleDateFormat like: "yyyMMdd".... – Opiatefuchs May 11 '15 at 12:40
  • SimpleDateFormat("yyyy.MM.dd") is wrong. SimpleDateFormat("yyyyMMdd") is what you need – Blackbelt May 11 '15 at 12:40
  • @Blackbelt I tried that also but i want in formate like `"19. January 2015"` . – GrIsHu May 11 '15 at 12:41
  • 2
    it's a two steps process. The first step is to `parse` your String back to a `Date` object. The second is to `format` the `Date` to a String. To format it back use a different `SimpleDateFormat` object – Blackbelt May 11 '15 at 12:43
  • @GrIsHu Where is your second simple date format in which you want your out put ??? – Piyush May 11 '15 at 12:44
  • @Blackbelt Can you explain by coding? – GrIsHu May 11 '15 at 12:52
  • Use the first `SimpleDateFormat` to parse. Use `SimpleDateFormat formatter = new SimpleDateFormat("dd. MMMM yyyy");` to call format: `String output = formatter.format(date);` – Blackbelt May 11 '15 at 12:56

2 Answers2

2

You will need to specify two formats: one to parse your input, one to format your output.

  1. The error occurs because the string you are trying to parse does not match the format you specified in originalFormat: that one needs to be

    SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
    

    if you want to parse strings of the format String m_date = "20150119";. Parsing a string with that format will give you a Date:

    Date date = originalFormat.parse(m_date);
    
  2. Then you may use another format to output your Date:

    SimpleDateFormat outputFormat = new SimpleDateFormat("dd. MMMM yyyy");
    System.out.println("Date: " + outputFormat.format(date));
    
dtk
  • 2,197
  • 2
  • 26
  • 19
1

You are using the DateFormat

SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy.MM.dd");

So SimpleDateFormat expects a String like "2015.01.19" (note the dots).

You're supplying the String

String m_date = "20150119";

which does not contain the dots therefore SimpleDateFormat cannot parse the String as it does not contain dots (as specified by you).

To parse your String you have to use

SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");

To print the parsed date you have to use aother SimpleDateFormat e.g.

SimpleDateFormat targetFormat = new SimpleDateFormat("dd. MMMM yyyy");

Then you can use the method format() to format your date in the format you want.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48