Praneel PIDIKITI
Praneel PIDIKITI

Reputation: 19537

Date format in java

      SimpleDateFormat sdf = new SimpleDateFormat("ddMMM", Locale.ENGLISH);

      try {

        sdf.parse(sDate);
      } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

My date format is 04AUG2011 and i want to have 20110804. So how can i do that ?

Upvotes: 0

Views: 10922

Answers (3)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181460

Use the following format:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);

I think you need to differentiate between parsing and outputting:

 SimpleDateFormat parseFormat = new SimpleDateFormat("MMMdd", Locale.ENGLISH);
 SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
 String dateStr = "06Sep";
 // parse with 06Sep format
 Date din = parseFormat.parse(dateStr);
 // output with 20101106 format
 System.out.println(String.format("Output: %s", outputFormat.format(din)));

Upvotes: 2

Buhake Sindi
Buhake Sindi

Reputation: 89209

Here's a complete working sample. I hope next time, you will do your own work.

/**
 * 
 */

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


/**
 * @author The Elite Gentleman
 *
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            String date = "06Sep2011";
            SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
            Date d = sdf.parse(date);

            SimpleDateFormat nsdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
            String nd = nsdf.format(d);
            System.out.println(nd);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Upvotes: 0

Manoj
Manoj

Reputation: 5612

Change your dateformat to

 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);

Upvotes: 0

Related Questions