GregorianCalendar gd1 = new GregorianCalendar(2013,12,12);
Date d = gd1.getTime();
Sun Jan 12 00:00:00 EST 2014 <===== what's wrong?
Is there a simpler way to construct Date for certain date (the Date(year, month, day) is deprecated).
Thanks!
The GregorianCalendar class' months are zero-based (many people see this as a mistake, but that's not the point).
If you actually want to put the date as December 12, 2013, you would have to use the following line:
GregorianCalendar gd1 = new GregorianCalendar(2013,11,12);
Related
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 3 years ago.
I'm using the new Date() function in order to get the current date. However, on the 29th of December, 2019 (29/12/2019), the function begins to display the 29th of December, 2020 (29/12/2020), incorrectly moving the year forward 3 days early.
It then reverts back to the correct year on the 1st of January (01/01/2020), correctly displaying the correct day, month and year.
I have absolutely no idea what is causing this, or why this happens.
Has anyone experienced this before and knows why? Or knows how to fix it?
My code for it is listed below. It's a pretty simple flow of events, but as mentioned above, as soon as the date reaches the 29th of December, the year in the TextView suddenly changes to 2020 instead of 2019. This happens in future years as well (2020 to 2021 etc.)
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY");
String currentDate = sdf.format(new Date());
startDateTextView.setText(currentDate);
Any help will be greatly appreciated :)
Note: Use modern java.time classes for all your date-time handling code instead of using Calendar and Date.
Coming to your problem
Here in your case, you need to use dd/MM/yyyy instead of dd/MM/YYYY in date formatter. Read Y returns 2012 while y returns 2011 in SimpleDateFormat for more details.
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = df.format(c);
Please use this to get current date
I'am using GregorianCalendar class to manipulate with date and time.
I need to get only a current date without time.
My code:
Calendar today = new GregorianCalendar();
today.set(GregorianCalendar.HOUR,0);
today.set(GregorianCalendar.MINUTE,0);
today.set(GregorianCalendar.SECOND,0);
today.set(GregorianCalendar.MILLISECOND,0);
Date todayDate = new Date();
todayDate.setTime(today.getTime().getTime());
I expect todayDate will be like this "Wed Dec 07 00:00:00 EET 2016"
But actually todayDate is "Wed Dec 07 12:00:00 EET 2016".
Which is the correct way to do it?
Ia understend difference between fields HOUR and HOUR_OF_DAY when I get value, but why when I set value of HOUR to "0" the HOUR_OF_DAY is not seting to "0" automatically. Zero is always zero...
Question is about Data integrity...
It is a mistake to thought zero is always zero... It is not true for hours after midday. Zero hours after midday in short form (am/pm) is 12 hours in 24 form.
Thanks to #selvin
In my application i have to show dates 5 years back dates from current date like example today is 4th July 2016 but in my date picker it will show as 4th July 2011 and user can not choose date after 4th July 2011 but can choose before 4th July 2011 and backwards.
I have seen some examples like Click here
DatePickerDialog mDate = new DatePickerDialog(DatePicker.this, date, year, month, day);
maxDate = Calendar.getInstance();
maxDate.add(Calendar.YEAR, -5);
mDate.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
mDate.show();
I have tried this and its working perfectly from January 1900 to July 4 2011 but one thing i want that is when i click the date in calendar it shows January 1900 all i want is when i click it it should show from July 2011 and then backward dates.
Solution-1
You can just set :
long years4 = 126144000; //4 years in seconds
long years_millis4 = years4*1000; //4 years in milliseconds, which is required format in date picker
dateDialog.getDatePicker().setMaxDate((new Date().getTime())-years_millis4);
where dateDialog is a
new DatePickerDialog()
and the param type to set for MaxDate and MinDate is a long
Hope this works for your case.
Apart from this you can add listeners to your date picker using which you can prompt user in case he chooses any date other than the required ones.
Solution 2
You can implement your custom date picker using spinners and custom dates as the list in the spinners. Apply proper conversions while getting the input.
Make sure that java.util.Date.getTime() method returns how many milliseconds have passed since January 1, 1970, 00:00:00 GMT.
I am using Calendar Class in my android app to calulate day of year of a date and then do some comparison with current day of year. Here is the code I use :
Date now=new Date();
Calendar ca1 = Calendar.getInstance();
ca1.set(now.getYear(), now.getMonth(), now.getDate());
int nowC=ca1.get(Calendar.DAY_OF_YEAR);
//Date arg0=say,get from user
Calendar ca2 = Calendar.getInstance();
ca2.set(arg0.GetBirthDay().getYear(),arg0.GetBirthDay().getMonth(),arg0.GetBirthDay().getDate());
int d1C=ca2.get(Calendar.DAY_OF_YEAR);
I debug my application and I see the following value for current day :
Fri Mar 02 14:18:33 Asia/Tehran 2012
and for arg0:
Fri Mar 02 00:00:00 Asia/Tehran 1979
And 'nowC' got 62, and 'd1C' got 61.
I expect them to be equal cause both of them has same month and day, also If I use DateTime class of joda package, as below, I get the same results:
int ndy=dtnow.getDayOfYear();
int d1dy=dt1.getDayOfYear();
Why it is happening ?
2012 was a leap year, 1979 was not. There is an extra day before March 2nd this year - so both APIs are giving you the right answer!
I suspect you want to compare both the month and day-of-month, to get the semantics you are expecting.
I am stuck in one date format
i want my date should look like this, 18th Mar 2011
and it can be 1st, 2nd,3rd that means i want to resolve for all the aspects
Plz help me out for this ASAP
Thanks in advance to all.
I think you want to change date in a 18th, 2nd, 1st, 3rd dated way, if i am not wrong then you can use simpleDateFormat class to convert date in different formats.
Before using SimpleDateFormat, just refer the SDK documentation: http://developer.android.com/reference/java/text/SimpleDateFormat.html.
To have day number with nd, th, rd (i.e. 2nd, 4th, 3rd, etc.), you can use:
F - day of week in month (Number) - 2 (2nd Wed in July)
(given in the documentation).
For example using SimpleDateFormat:
String dateStr = "03/08/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
This is the term you are looking for: Quantity Strings (Plurals)
here is a link for it in the documentation:
Plurals
Here is a link with examples:
Examples
And One more:
Android Pluralization not working, need help
Hope this helps. so you just need to reformat the string with the date by using the examples provided and thats it.