Change in Calendar date by default [duplicate]










0
















This question already has an answer here:



  • Why are months off by one with Java SimpleDateFormat?

    5 answers



  • Why is January month 0 in Java Calendar?

    16 answers



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?










share|improve this question















marked as duplicate by Ole V.V., Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 16:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 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 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 9:01
















0
















This question already has an answer here:



  • Why are months off by one with Java SimpleDateFormat?

    5 answers



  • Why is January month 0 in Java Calendar?

    16 answers



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?










share|improve this question















marked as duplicate by Ole V.V., Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 16:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 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 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 9:01














0












0








0









This question already has an answer here:



  • Why are months off by one with Java SimpleDateFormat?

    5 answers



  • Why is January month 0 in Java Calendar?

    16 answers



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?










share|improve this question

















This question already has an answer here:



  • Why are months off by one with Java SimpleDateFormat?

    5 answers



  • Why is January month 0 in Java Calendar?

    16 answers



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?





This question already has an answer here:



  • Why are months off by one with Java SimpleDateFormat?

    5 answers



  • Why is January month 0 in Java Calendar?

    16 answers







java android date-difference android-date java.util.calendar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:03









Ole V.V.

29.9k63655




29.9k63655










asked Nov 14 '18 at 8:18









JustABeginnerJustABeginner

11611




11611




marked as duplicate by Ole V.V., Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 16:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Ole V.V., Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 16:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 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 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 9:01













  • 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 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 9:01








1




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 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 9:01






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 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 9:01













1 Answer
1






active

oldest

votes


















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());





share|improve this answer

























  • 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 9: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 9:22











  • I did the same thing, but stackoverflow.com/users/5772882/ole-v-v answer was more than satisfying!

    – JustABeginner
    Nov 14 '18 at 10:38

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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());





share|improve this answer

























  • 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 9: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 9:22











  • I did the same thing, but stackoverflow.com/users/5772882/ole-v-v answer was more than satisfying!

    – JustABeginner
    Nov 14 '18 at 10:38















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());





share|improve this answer

























  • 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 9: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 9:22











  • I did the same thing, but stackoverflow.com/users/5772882/ole-v-v answer was more than satisfying!

    – JustABeginner
    Nov 14 '18 at 10:38













0












0








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());





share|improve this answer















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());






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 9:15

























answered Nov 14 '18 at 9:10









Sultan MahmudSultan Mahmud

23017




23017












  • 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 9: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 9:22











  • I did the same thing, but stackoverflow.com/users/5772882/ole-v-v answer was more than satisfying!

    – JustABeginner
    Nov 14 '18 at 10:38

















  • 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 9: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 9:22











  • I did the same thing, but stackoverflow.com/users/5772882/ole-v-v answer was more than satisfying!

    – JustABeginner
    Nov 14 '18 at 10:38
















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 9:21





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 9:21




1




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 9:22





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 9:22













I did the same thing, but stackoverflow.com/users/5772882/ole-v-v answer was more than satisfying!

– JustABeginner
Nov 14 '18 at 10:38





I did the same thing, but stackoverflow.com/users/5772882/ole-v-v answer was more than satisfying!

– JustABeginner
Nov 14 '18 at 10:38





Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus