I can't set TimeZone to Calendar - java

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.

Related

Why is Calendar class (Java) resetting its fields?

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

Android , Calendar.getInstance() not giving the correct month

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

Get the Date into parameters as integers

How can I get the day_of_month, month and the year into parameters as integers?
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar start_date = Calendar.getInstance();
System.out.println(dateFormat.format(start_date.getTime()));
int day = start_date.get(Calendar.DAY_OF_MONTH);
int month = start_date.get(Calendar.MONTH);
int year = start_date.get(Calendar.YEAR);
String result = day + "/" + month + "/" + year;
but, the date today is: 01/12/12, while the result is: 01/11/12.
This is expected behaviour. Month goes from 0 to 11. You can check this by issuing:
System.out.println(Calendar.JANUARY);
Calendar
Calendar.MONTH in particular
By the way, to convert dates to Strings, you should really use SimpleDateFormat
new SimpleDateFormat("dd/MM/yyyy").format(start_date.getTime());
(sidenote, but important: SimpleDateFormat is not thread safe very much not thread safe, so don't try to optimize by using them as static instances in a multithreaded environment! This is also stated in the API doc: Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.)
month index starts from 0, thats why gives 11
0- january
.
.
.
11-december
In Calendar class Month starts with 0 which is January and ends with 11 which is December.
Open JDk 6-
public final static int JANUARY = 0;
...
public final static int DECEMBER = 11;
From the documentation of MONTH (the emphasis is mine):
MONTH: Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
Human calendars start January at one, not at zero. That is why December ends up at eleven instead of twelve.
Just don't use java.util.Date and other bad parts of java
From Java 8 joda time will be in standard library so I do not see point in using bad code.
http://joda-time.sourceforge.net/
You will not have issues with this one, trust me.
According to the documentation, the result of start_date.get(Calendar.MONTH) is a int ranging from 0 to 11, where 0 is January and 11 is December.
Calendar.MONTH

Problems with the Date 31-12-9999 in java

My program needs to represent this date as a java.sql.date object , but it seems that when I create a new date (using the calendar) and set it to '9999-12-31' and finally convert this java.util.date object to a java.sql.date object, this date is converted to something like '000-01-31'.
Calendar calendar = Calendar.getInstance();
calendar.set(9999, 12, 31);
infinityDate = new java.sql.Date(normalizeDate(calendar.getTime()).getTime());
infinityDate should be 31-12-9999
but when my code reaches here :
if(otherDate.equals(infinityDate))
{// Do stuff}
It never goes into the if condition as the infinityDate has for some reason been changed to 31-01-000, even though otherDate is infact '31-12-9999'.
The fact that otherDate is 31-12-9999 tells me that java can represent this dates , but for some reason , when I construct it using a calendar it changes the date. (otherDate comes from a jdbc statement which fetches data from a database)
This reference date '31-12-9999' has been fixed by some client , so it cannot be changed and my program has to be able to compare some incoming date values with this.
Does anyone know why this is happening , I realize that http://en.wikipedia.org/wiki/Year_10,000_problem may be a problem for dates after year 9999 , but I should be safe by a day.
EDIT : The Normalize date method only "normalizes the given date to midnight of that day"
private static java.util.Date normalizeDate(java.util.Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
date = calendar.getTime();
return date;
}
But , this issue was appearing before I was normalizing the date , I normalized it in an attempt to fix this.
Months are zero indexed. Use 11 for December, not 12. This is why you are rolling over the year.
Calendar.MONTH is zero-based. The call
calendar.set(9999, 12, 31);
sets the date to "the 31st day in the 13th month of the year 9999", which is then implicitly converted to the 1st month of the year 10000. It would result in an exception if you first called
calendar.setLenient(false);
Check hours, minutes, seconds and milliseconds that are held into these 2 date objects. I believe they are different.
If your want to compare the date (year, month, day) only you should probably create your custom Comparator and use it.

In java, Date() method returns a value. but I am confused in that

I used Date() for getting the date of my birthday, but it was returned the mismatch of the month. My birthday is 04-March-87. so i gave an input as,
Date birthDay= new Date(87,03,04,8,30,00);
But it returns correct year, day and time. But month was problem. It displays April month.
What's wrong with that?
Months are set from 0 to 11, January =0, February = 1, ..., December = 11.
So, for April do this:
Date birthDate = new Date(87,02,04,8,30,00); //March = 2
Hope this helps;
EDIT
The Date class with this constructor public Date(int year, int month, int date, int hrs, int min) is deprecated (i.e. it has been declared #Deprecated).
Rather do this.
Calendar calendar = Calendar.getInstance();
Calendar.set(1987, 2, 4, 0, 0, 0);
Date birthDate = calendar.getTime();
(It will return the same thing as what you asked for)
The month in the Date class starts with 0 for January, so March is 2, not 3.
Also, the Date(int, int, int, int, int, int) constructor is deprecated, so you should consider using the Calendar class instead.
Finally, be careful with leading zeros in Java - they represent octal constants. The number 09 would not do what you expect.

Categories