0
Item i1=new Item();
        int month3=0;
          i1.setManufacturingDate(rs.getDate("mfg_date"));
          Date date1 = rs.getDate("mfg_date");
          Calendar cal = Calendar.getInstance();
          cal.setTime(date1);
          int month = cal.get(Calendar.MONTH);
          int date=cal.get(Calendar.DATE);
          int year=cal.get(Calendar.YEAR);
          int month2=month+1;
         // System.out.print("\t"+month2);


        i1.setUseBeforeMonths(rs.getInt("UseBeforeInMonths"));
        int month1=rs.getInt("UseBeforeInMonths");
        //System.out.print("\t"+month1);

        System.out.println("Expiry Date is");
        int expiry=month2+month1;
        int sum=0;
        if(expiry>12)
        {
            sum=expiry-12;
            System.out.println(sum);
            month3=sum;
        }
        else
        {
            System.out.println(expiry);
            month3=expiry;
        }

This code calculates the expiry date. But I dont know how to convert it into date format.

I tried this

Calendar c3 = Calendar.getInstance();
                c3.set(year, month3, date); 
                System.out.println(c3);

but it doest not give proper output. Output given by it is

Expiry Date is
1
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=1,WEEK_OF_YEAR=28,WEEK_OF_MONTH=2,DAY_OF_MONTH=9,DAY_OF_YEAR=191,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=34,SECOND=39,MILLISECOND=138,ZONE_OFFSET=19800000,DST_OFFSET=0]
Expiry Date is
4
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=4,WEEK_OF_YEAR=28,WEEK_OF_MONTH=2,DAY_OF_MONTH=10,DAY_OF_YEAR=191,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=34,SECOND=39,MILLISECOND=139,ZONE_OFFSET=19800000,DST_OFFSET=0]
Expiry Date is
11

    java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=11,WEEK_OF_YEAR=28,WEEK_OF_MONTH=2,DAY_OF_MONTH=28,DAY_OF_YEAR=191,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=34,SECOND=39,MILLISECOND=139,ZONE_OFFSET=19800000,DST_OFFSET=0]

Please help me to convert date in Date format i.e 'YYYY-MM-DD'. here date variable has date, month3 variable has month and year variable has year

Thank you.

sss
  • 49
  • 8
  • possible duplicate of [Using GregorianCalendar with SimpleDateFormat](http://stackoverflow.com/questions/10829942/using-gregoriancalendar-with-simpledateformat) – pjanssen Jul 10 '15 at 16:22
  • @sss Please search StackOverflow before posting. This topic has been addressed many many many times before. – Basil Bourque Jul 10 '15 at 18:27

4 Answers4

0

Edited due to misinterpretation:

GregorianCalendar dateConverted = new GregorianCalendar(year, month, days);
format(dateConverted);
return dateConverted;

Good luck, tell me if this works.

Going hamateur
  • 421
  • 3
  • 4
  • It does not work:( and i want to create expiry date in date format – sss Jul 10 '15 at 16:29
  • Sorry to hear potentially: GregorianCalendar dateConverted = new GregorianCalendar(year, month3, day); format(dateConverted); return dateConverted; – Going hamateur Jul 10 '15 at 16:33
  • format(dateConverted) returns the value of type string. I want it in Date format. i.e. Date date – sss Jul 10 '15 at 16:58
0

Try Using an Implementation of Java 8's LocalDate and DateTimeFormatter classes:

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; // The Built-in YYYY-MM-DD format
LocalDate date = LocalDate.of(c3.get(Calendar.YEAR), c3.get(Calendar.MONTH), c3.get(Calendar.DATE));
System.out.println(formatter.format(date));

Edit for pre-Java 8:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.format(c3.getTime()));
BestGamer
  • 11
  • 4
0

Simplest n easiest method is

Date object=new Date();

SimpleDateFormat myformat = new SimpleDateFormat("ddmmyyyy"); String mydate = myformat.format(object);

Here date will return us current date .using format we fetch dd: date mm: month yy: year

The benefit of this is u can get time also by specifying("HHMMSS") for hours minutes and seconds.

Vishesh
  • 308
  • 1
  • 9
0
Date aDate = new Date(year1,month3,date);
                System.out.println(aDate);

This is what I found easy.

Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
sss
  • 49
  • 8