1

How can I input "1502009" and get the output "January 15, 2009"?

I have read a lot of the date questions on SO but I'm still finding it hard to get the best and fastest way to implement this specific format.

Any help appreciated.

EDIT: My sample string is coming from a dialog box date picker I have in my android app. The user picks the day month and year and it returns the different ints. January is represented as a 0. So in my app, I want 1502009 to represent January 15, 2009.

Ogen
  • 6,499
  • 7
  • 58
  • 124

4 Answers4

3
use the following code to convert into the required format

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class dateconversion {
    /**
     * @param args
     */
    public static void main(String[] args) {
        String maxDate = "15012009";
        SimpleDateFormat fromFormat = new SimpleDateFormat("ddMMyyyy");
        SimpleDateFormat toFormat = new SimpleDateFormat("MMMM dd, yyyy");
        Date date = null;
        try {
            date = fromFormat.parse(maxDate);
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("formated date:-" + toFormat.format(date));
    }
}
pnathan
  • 713
  • 3
  • 9
  • `"1512009"` is giving me `formated date:-December 15, 0009` when it should be giving me `January 15, 2009` – Ogen Oct 11 '13 at 10:21
  • I have done that code for two digits month. If you want for the one digit month then you have to just change the fromFormat in the code with the following code. SimpleDateFormat fromFormat = new SimpleDateFormat("ddMyyyy"); – pnathan Oct 11 '13 at 10:29
  • I modified your code to get me the perfect answer, do you mind if i edit ur answer and then accept it? – Ogen Oct 11 '13 at 10:40
  • ya sure you can do that. clay – pnathan Oct 11 '13 at 10:42
0

Try following simple code:

public static void main(String[] args) throws ParseException {
    String str="1502009";
    SimpleDateFormat format=new SimpleDateFormat("DDMyyyy");
    SimpleDateFormat resformat=new SimpleDateFormat("MMMM DD, yyyy");
    Date date =format.parse(str);
    System.out.println(resformat.format(date));
}
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • I have edit my answer. This will be applicable for 1 digit month. If you need to have 2 digit month than new pattern will be needed. – Masudul Oct 11 '13 at 10:08
  • *January 15, 0009* is the output. OPs string is faulty. Why would you give a solution for that? – Rahul Oct 11 '13 at 10:09
  • if the input is like 15112009 what it gives? – newuser Oct 11 '13 at 10:09
  • @Masud Thanks for the reply. it IS working for one digit months but it's not working for 2 digit months. Im going to work on it to try to fix it :) – Ogen Oct 11 '13 at 10:12
0

I would suggest you take 3 integer variables and using Integer.parseInt and substring calculate all the 3 fields and then do the mapping. While calculating output string again you can add all the individual strings.

I am saying this because your input doesn't seem to be right.

Ankit Jain
  • 2,230
  • 1
  • 18
  • 25
  • I can do this as a last resort. I was just wondering if there was an easier way. Please see my edit to see where i got my input from. – Ogen Oct 11 '13 at 10:18
0

So the month can be in the range [0-11] right?

I dont think this is a good idea. To make it easy for you to parse the string you need to have a defined/constant width for each of the fields so that you can apply the pattern ddMMyyyy. Otherwise you'll have to find a way to figure out the width of each part in order to correctly parse it, which can be tricky. For example if you have 1112008, is it January 11, 2008 or December 1, 2008?

I would suggset you correct your date picker to return a date string representation which is easy to parse:

@Test
public void formatDateFromPicker() throws ParseException {
    // values from date picker
    int day = 15;
    int month = 0;
    int year = 2009;

    // build the easy to parse string from date picker: 15012009
    String strDate = String.format("%02d%02d%04d", 
            day, (month+1 /* add 1 if months start at 0 */), year);
    System.out.println(strDate);

    // parse the date string from the date picker
    Date date = new SimpleDateFormat("ddMMyyyy").parse(strDate);

    // ouput January 15, 2009
    System.out.println(new SimpleDateFormat("MMMM dd, yyyy").format(date));
}
A4L
  • 17,353
  • 6
  • 49
  • 70