0

I am having a strange problem here in android project. I am trying to convert the date into other date and for that, I am calculating the days between two dates. So, here is how I have implemented.

Calendar baseEngDate = new GregorianCalendar();
Calendar currentEngDate= new GregorianCalendar();
baseEngDate.set(startingEngYearForSelection, startingEngMonthForSelection, startingEngDayForSelection);
currentEngDate.set(engYear, engMonth, engDay);
long totalEngDaysCount = daysBetween(baseEngDate, currentEngDate);

This is my baseEngDate Calendar. The set method has 3 parameters which have following parameters and their default values are:

int startingEngYearForSelection = 1944;
int startingEngMonthForSelection = 1;
int startingEngDayForSelection = 1;

But, when I try to check the value of them in this function,

 private long daysBetween(Calendar startDate, Calendar endDate) {

        SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
        String inputString1 = null;
        String inputString2 = null;

        Date date1 = startDate.getTime();
        Date date2 = endDate.getTime();

        inputString1 = myFormat.format(date1);
        inputString2 = myFormat.format(date2);
        Log.e("String", inputString1); 
.
.
.

    }

I get this value in log, which is not what I expected to be:

2018-11-14 13:55:29.982 15712-15712/com.utilnepal E/String: 01 02 1944

It should have returned me 01 01 1944 but it returns 01 02 1994. why?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
JustABeginner
  • 785
  • 2
  • 11
  • 26
  • 1
    This is one of the many troubles with the age-old `Calendar` class. The good solution is to throw it overboard along with `Date` and the notoriously troublesome `SimpleDateFormat` and instead add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. And has much better support for counting days between two dates. – Ole V.V. Nov 14 '18 at 09:01

1 Answers1

0

apply this and get your date in desired format:

In Java Calender January is 0 and others so on increment 1  

startingEngMonthForSelection will be 0 instead of 1;

 int startingEngYearForSelection = 1944;
 int startingEngMonthForSelection = 0;
 int startingEngDayForSelection = 1;

long diff = endDate.getTimeInMillis() - startDate.getTimeInMillis(); 

     SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(diff);
     String date=formatter.format(calendar.getTime());
Sultan Mahmud
  • 1,245
  • 8
  • 18
  • In case of a duplicate question (as I believe this one is) I think it’s better to vote to close than to provide a new answer giving the same information already found with the original answer. If you believe information is missing from the original, please provide an answer there instead. In the end it will help everyone find the information they need. – Ole V.V. Nov 14 '18 at 09:21
  • 1
    If you do insist on using the poorly designed and outdated `Calendar` class, at least don’t use `0` for January, use `Calendar.JANUARY` for readability. – Ole V.V. Nov 14 '18 at 09:22
  • I did the same thing, but https://stackoverflow.com/users/5772882/ole-v-v answer was more than satisfying! – JustABeginner Nov 14 '18 at 10:38