Question

I have to parse "17-Jun" format date using Java.But the problem is when I try to parse "dd-MM" format using SimpleDateFormat it is returning as "Wed Jun 17 00:00:00 IST 1970".Is it possible to get current(2014) year instead of 1970.

My result:
17/JUNE/1970

Expected result:
17/JUNE/2014

Was it helpful?

Solution

Have a look at this..

Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 17);
c.set(Calendar.MONTH, 5);
c.set(Calendar.YEAR, c.get(Calendar.YEAR));
Date date=new Date(c.getTimeInMillis());
SimpleDateFormat simpleDateformatter = new SimpleDateFormat("dd/mmm/yyyy");
String convertedDate = simpleDateformatter .format(date);

To get year you can just use

Calendar cal = Calendar.getInstance();
cal.get(Calendar.YEAR) will fetch you current year

Hope it helped... :)

OTHER TIPS

Try this

    Calendar c = Calendar.getInstance();
        c.set(Calendar.DATE, 17);
       c.set(Calendar.MONTH, 5);
    c.set(Calendar.YEAR, c.get(Calendar.YEAR));
Date d=new Date(c.getTimeInMillis());
    SimpleDateFormat formatter = new SimpleDateFormat("dd- mmm");
        String conDate = formatter.format(d);

Do like this

Date date = new SimpleDateFormat("dd-MMM-yyyy").parse("17-Jun-"+ Calendar.getInstance().get(Calendar.YEAR));

You'll have to write a utility method, there isn't anything in SimpleDateFormat that will interpret a non-existant year as the current year. Something like this:

public static Date parseDate(String dateString) throws ParseException {

    //determine current year
    Calendar today = Calendar.getInstance();
    int currentYear = today.get(Calendar.YEAR);

    //parse input
    SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
    Date parsed = format.parse(dateString);

    // set current year on parsed value
    Calendar cal = Calendar.getInstance();
    cal.setTime(parsed);
    cal.set(Calendar.YEAR, currentYear);

    return cal.getTime();

}

Try this:

SimpleDateFormat dfDate  = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date d = null;

try {
                d = dfDate.parse("17-Jun-"+ Calendar.getInstance().get(Calendar.YEAR));

            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
System.out.println(""+d );

your problem will be solved.

java.time

In Java 8 you can do something like:

   DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM");
   MonthDay md = MonthDay.parse("17-Jun", dtf);
   LocalDate d = LocalDate.now().with(md);
   System.out.println(d.getDayOfMonth());
   System.out.println(d.getMonthValue());
   System.out.println(d.getYear());

I guess the simplest way is to do this:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MMM/dd");
Date date = new Date();
System.out.println("Time is: " + dateFormat.format(date) );

This gives you exactly what you want. also see

http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html

Little late, but if you really don't want to use Calendar at all - as I gather from your comments to the correct answers above - (not recommended with the usage of deprecated methods, but still):

SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
Date date = format.parse("17-JUN");
date.setYear(new Date().getYear());
System.out.println(date);

Output:

Tue Jun 17 00:00:00 IST 2014

All answers given here are more or less correct, but I notice that one detail aspect is still overlooked, namely if the combination of day and months fits to current year (february 29 problem). So I would suggest a strict parsing like following:

String ddMMM = "17-Jun";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
sdf.setLenient(false); // in order to check for "29-Feb"
TimeZone tz = TimeZone.getDefault(); // or change to your specific time zone
Date date = 
  sdf.parse(ddMMM + "-" + new GregorianCalendar(tz).get(Calendar.YEAR));

Try,

    String s2 = "Wed Jun 17 00:00:00 1970";
    SimpleDateFormat sdf1 = new SimpleDateFormat("E MMM dd hh:mm:ss yyyy");
    SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MMM/yyyy");
    try {

        Date d1 = sdf1.parse(s2);
        System.out.println(d1);

        String s3 = sdf2.format(d1);
        System.out.println("Before Changing :: "+s3);

        Calendar cal = Calendar.getInstance();
        cal.setTime(d1);
        cal.add(Calendar.YEAR, 2014-1970);
        d1 = cal.getTime();
        String s4 =  sdf2.format(d1);
        System.out.println("After Changing  :: "+s4);

    } catch (ParseException e) {
        e.printStackTrace();
    }

Output

Before Changing :: 17/Jun/1970
After Changing  :: 17/Jun/2014
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top