1

I require this format MonthDay, example 0422

I'm creating

SimpleDateFormat sdf = new SimpleDateFormat("MMdd");

and giving it the current month and current day

curDate = sdf.parse(curMon+""+curDay);

but I'm getting this format:

Thu Jun 07 00:00:00 CEST 1973

What do I need to do?

Rahul Bobhate
  • 4,551
  • 2
  • 22
  • 48
user2425161
  • 11
  • 1
  • 1
  • 2
  • 1
    A standard `Date` has no format. If you use `System.out.println(someDate)`, it just prints the date object with the default formatting (as in your example), irrespective of how you created that object. – Vincent van der Weele May 27 '13 at 13:32
  • 2
    if you already have curMon and curDay, why do you need a SimpleDateFormat ? – Blackbelt May 27 '13 at 13:34

3 Answers3

3

Instead of using parse, use format as follows:

SimpleDateFormat s = new SimpleDateFormat("MMdd");
String d = s.format(new Date());
System.out.println(d);

This will generate, since today is 27th May:

0527               
Rahul Bobhate
  • 4,551
  • 2
  • 22
  • 48
2

I hope below code will help you...

    strDate="2014-08-19 15:49:43";

    public String getMonth(String strMonth) {

    int month = Integer.parseInt(strMonth.substring(0, 2));
    int day = Integer.parseInt(strMonth.substring(strMonth.length() - 2,
            strMonth.length()));

    String d = (new DateFormatSymbols().getMonths()[month - 1]).substring(
            0, 3) + " " + day;
    return d;
}


 public static String smallDate(String strDate) {

    String str = "";
    try {
        SimpleDateFormat fmInput = new SimpleDateFormat("yyyy-MM-dd");
        Date date = fmInput.parse(strDate);

        SimpleDateFormat fmtOutput = new SimpleDateFormat("MMdd");
        str = fmtOutput.format(date);

        str = getMonth(str);
         Log.d("Output date: "+str);
        return str;


    } catch (Exception e) {

    }
}
Ganesh Katikar
  • 2,460
  • 1
  • 21
  • 27
0

use like that

Date date; // your date
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        String month = cal.get(Calendar.MONTH).toString();
        String day = cal.get(Calendar.DAY_OF_MONTH).toString();
        String mmdd=month+""+day; 
Sunil Kumar
  • 7,027
  • 4
  • 30
  • 48