simpledateformat for month and day in month only

11,333

Solution 1

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               

Solution 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) {

    }
}
Share:
11,333
user2425161
Author by

user2425161

Updated on June 04, 2022

Comments

  • user2425161
    user2425161 almost 2 years

    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?

    • Vincent van der Weele
      Vincent van der Weele almost 11 years
      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.
    • Blackbelt
      Blackbelt almost 11 years
      if you already have curMon and curDay, why do you need a SimpleDateFormat ?