I am trying to set properties for a Calendar class instance. But each time I get to Dec 30, it resets to the next year. Is this a draw in the Calendar class?
public Calendar setCalendar()
{
String Date = "2013-12-30";
int yyyy = Integer.parseInt(date.substring(0,4));
int mm = Integer.parseInt(date.substring(5,7));
int dd = Integer.parseInt(date.substring(8,10));
System.out.println(yyyy + " " + mm + " " + dd);
Calendar Cal= new GregorianCalendar();
Cal.set(yyyy,mm,dd);
System.out.println(Cal.get(Calendar.YEAR)+","+Cal.get(Calendar.MONTH));
return Cal;
}
Output:
2013 12 30
2014,0
With Calendar the months are 0-based, so you're actually setting the month to next year's January.
If you really need to use a Calendar I recommend at least using a SimpleDateFormat to parse your String to a Date and setting the calendar using that.
Check the Calendar API doc, month value is 0 based i.e Jan is 0.
month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
You are setting month to 12. This effectively gives you the next year. To correct try:
Cal.set(yyyy,mm-1,dd);
Hope that helps
Related
Hello guys i have a tricky question for you that i really cant find a solution out there.
What i want to do is to have 3 date/time inputs on simpledateformat
Date 1
Date 2
Date 3
and basicaly i want to get difference of months days hours and minutes from date 1 - date2 and result of those 2 dates to be added on the firth date
for example 11/3/2017 12:30 - 7/3/2017 = 4 days and ADD that to current date 13/3/2017 13:30 + 4 days and 1 hour = 17/3/2017 14:30
i know how to get the diference in days hours and minutes , i cant get the second part of adding the result to the current date
any ideas?
thank you in dvance
Use Calendar class to add days and hours
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.add(Calendar.DAY_OF_MONTH, yourDays); //adds days to your date
cal.add(Calendar.HOUR_OF_DAY, yourHours); //adds hours to your date
cal.getTime(); //to get Date instance
To decrement dates just add negative number, for example:
int yourDays = -daysVariable;
int yourHours = -hoursVariable; //
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.add(Calendar.DAY_OF_MONTH, yourDays); //decrement days
cal.add(Calendar.HOUR_OF_DAY, yourHours); //decrement hours
cal.getTime(); //to get Date instance
Get a date object using the simpledate format
Because it is an object of the same type
Comparison is possible
I think you need to use getTime and add days throught miliseconds.
For example, if you can get the difference of days you should use something like this
date1.getTime() + 24*60*60*1000*4
(where 4 is the difference you want to add)
You can use Calendar too.
Thank you all for your ultra fast replies!
I solved it by using Mij Solution
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.add(Calendar.DAY_OF_MONTH, yourDays); //adds days to your date
cal.add(Calendar.HOUR_OF_DAY, yourHours); //adds hours to your date
cal.getTime(); //to get Date instance
To add days and
tis code to decrese days
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.add(Calendar.DAY_OF_MONTH, -yourDays); //adds days to your date
cal.add(Calendar.HOUR_OF_DAY, -yourHours); //adds hours to your date
cal.getTime(); //to get Date instance
and to control if date is bigget than my current date i used this condition
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {
catalog_outdated = 1;
}
it returns -1 if the date is past from current
+1 if date is bigger that current or whatever
and 0 if dates are equal
again thank you!
I'll be using the Calendar api for this. My primary concern is that
Date birthDate = (...say Apr 20th 0300hrs)
Calendar cal = Calendar.getInstance();
cal.setTime(birthDate);
cal.add(Calendar.HOUR, -6);
Date newDate = cal.getTime();
Will newDate be Apr 19th 2100hrs (9 PM) ?
No.
After running the following code:
Date birthDate = (...say Apr 20th 0300hrs)
Calendar cal = Calendar.getInstance();
cal.setTime(birthDate);
cal.add(Calendar.HOUR, -6);
you are guaranteed that cal is set to six hours before 'Apr 20th 0300hrs', but not that it is set to 'Apr 19th 2100hrs'.
Yes.
Reference for calendar.add
http://www.tutorialspoint.com/java/util/calendar_add.htm
Second parameter in Calendar's .add method is indeed the delta, or the change, so yes, this would subtract 6 hours.
read more here:
enter link description here
I am trying to write code to find the Day difference between tow date but Calendar.getInstance() keep getting the date for previous month instead of current month
for example :Current 17/7/2014 it get 17/6/2014
my code :
TextView textview=(TextView) findViewById (R.id.textView1);
Calendar cal = Calendar.getInstance();
Calendar startDate=Calendar.getInstance();
startDate.set(Calendar.DAY_OF_MONTH, 1);
startDate.set(Calendar.MONTH,1);
startDate.set(Calendar.YEAR, 2013);
long diff=(((cal.getTimeInMillis()-startDate.getTimeInMillis())/(1000*60*60*24))+1);
String sdiff=String.valueOf(diff);
String stt=cal.get(Calendar.YEAR) +"_"+cal.get(Calendar.MONTH)+"_"+cal.get(Calendar.DAY_OF_MONTH);
textview.setText(stt);
Months start at 0, not at 1, but you really don't have to worry about this if you don't use magic numbers when getting or setting month but instead use the constants. So not this:
startDate.set(Calendar.MONTH,1); // this is February!
but rather
startDate.set(Calendar.MONTH, Calendar.JANUARY);
Months in Java's Calendar start with 0 for January, so July is 6, not 7.
Calendar.MONTH javadocs:
The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0
Add 1 to the result of get.
(cal.get(Calendar.MONTH) + 1)
This also affects your set call. You can either subtract 1 when passing a month number going in, or you can use a Calendar constant, e.g. Calendar.JANUARY.
You can also use a SimpleDateFormat to convert it to your specific format, without having to worry about this quirk.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
String stt = sdf.format(cal.getTime());
I don't understand output of following code is 12- 12-1991,
Please explain for me, Thank for your help
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+7"));
SimpleDateFormat simpleDF = new SimpleDateFormat("dd-MM-yyyy");
c.set(1991, 11, 12);
Date d = c.getTime();
System.out.println(simpleDF.format(d));
months is zero index based. You need to have 10 instead of 11.
Alternatively instread of using integers directly, you can write meaningfully.
c.set(1991,Calendar.NOVEMBER, 12);
Where Calendar.NOVEMBER is a static int field which represents NOVEMBER.
January is Month 0, so when you set 11 that is December
Check java.util.Date docs
Month value is 0-based. e.g., 0 for January.
You have to change
c.set(1991, 11, 12);
To
c.set(1991, 10, 12);
From javadocs:
set
public final void set(int year,
int month,
int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH. Previous values of other calendar fields are retained. If this is not desired, call clear() first.
Parameters:
year - the value used to set the YEAR calendar field.
month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DAY_OF_MONTH calendar field.
Have next function to get week of year:
static public Integer getWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setMinimalDaysInFirstWeek(1);
cal.setTime(date);
Integer week = cal.WEEK_OF_YEAR;
Integer month = cal.MONTH;
if ((week == 1) && (month == 12)) week = 52;
return week;
}
Call the function with date=02.01.2013
What I see in debug:
date = Wed Jan 02 00:00:00 SAMT 2013
week = 3
month = 2
I want to get: week=1, month=1. Right?
Where am I wrong?
JRE 1.6
Thanks a lot for advance.
Calendar.WEEK_OF_YEAR and Calendar.MONTH are static constants Calendar uses to look up fields. You want
Integer week = cal.get(Calendar.WEEK_OF_YEAR);
Integer month = cal.get(Calendar.MONTH);
Also, note that (I think) January is considered month 0.
public final static int WEEK_OF_YEAR = 3; is inside source of Calendar.
When you access WEEK_OF_YEAR it prints value of this field, it would be treated like accessing static field of Calendar class.
If you want get week, you need to do Integer week = cal.get(Calendar.WEEK_OF_YEAR);