Setting the year in Calendar [duplicate] - java

This question already has answers here:
java.util.Date is generating a wrong date?
(3 answers)
Closed 6 years ago.
I have the following piece of code.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY HH:mm");
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, 2016);
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DAY_OF_MONTH, 31);
cal.set(Calendar.HOUR_OF_DAY, 22);
Date start = cal.getTime();
System.out.println(dateFormat.format(start));
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 5);
Date end = cal.getTime();
System.out.println(dateFormat.format(end));
It prints:
31/12/2016 22:00
01/01/2016 05:00
I expect that the year of the second date is 2017. What is going on? I'm using Java 1.7.

The correct date format should be dd/MM/yyyy HH:mm, not dd/MM/YYYY HH:mm, note the lower case y.
With that it works correctly.
From the docs:
y Year
Y Week year
Explanation of the difference between year and week year (from here):
A week year is a year where all the weeks in the year are whole weeks.
[...] Basically, this guarantees that a program working on a week's
data will not transition between years. [...] this also means that the beginning of the year may not start on the first of January.

This is working fine with the following dataFormat.
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm");

Related

Adding 1 hour to java.util.Calendar object(31-DEC-2015 23:00:00) gives 01-JAN-2015 00:00:00 2015 [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
This is my Java code
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.set(Calendar.YEAR, 2015);
c.set(Calendar.MONTH, 11);
c.set(Calendar.DAY_OF_MONTH, 31);
c.set(Calendar.HOUR_OF_DAY, 22);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-YYYY HH:mm:ss");
System.out.println("date is : "+sdf.format(c.getTime()));
c.add(Calendar.HOUR_OF_DAY, 1);
System.out.println("date2 is : "+sdf.format(c.getTime()));
and here is the output
date is : 31-Dec-2015 23:00:00
date2 is : 01-Jan-2015 00:00:00
Why is the year not changing to 2016 in the 2nd print statement?
I tried your code and it is working as you need, but you should change the format to
dd-MMM-yyyy HH:mm:ss

java.sql.Date is taking wrong date [duplicate]

This question already has answers here:
java.util.Date to java.sql.Date conversion gives wrong month
(2 answers)
Closed 8 years ago.
I am using below code,
private static Date date = new Date (2014-1900,11,25);
System.out.println(date);
It is displaying 2014-12-25. I am unable to understand why it is giving me date as 12?
and if i give
private static Date date = new Date (2014-1900,12,25);
it is returning 2015-01-25.
Can anyone help in comprehend this?
Calendar
It accept December month as 11 because month starts from 0 - 11
First you should not use this Constructor, because it is deprecated.
Second: See the documentation of this consturctor:
Parameters:year -
the year minus 1900.month - the month between 0-11.date - the day of
the month between 1-31.See Also:Calendar
month is a null based value, so 0 --> Jan ... 11 --> Dec
from java docs,
Parameters:
year the year minus 1900.
month the month between 0-11.
date the day of the month between 1-31.
Month's range from 0-11, ie Jan - Dec
Avoid using the depriciated Date() constructor for setting dates, It is recommended to use Calendar class
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, Calendar.NOVEMBER);
calendar.set(Calendar.DAY_OF_MONTH, 25);
Date date = calendar.getTime();
You can also use the simpleDateFormat for setting/formatting date values:-
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse("25-11-2014");

Getting UTC time with Calendar and Date [duplicate]

This question already has answers here:
Calendar.getInstance(TimeZone.getTimeZone("UTC")) is not returning UTC time
(7 answers)
Closed 8 years ago.
I'm trying to get an instance of Date with UTC time using the following code:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date now = cal.getTime();
that looks so simple, but if I check the values at IntelliJ's debugger, I get different dates for cal and now:
cal:java.util.GregorianCalendar[time=1405690214219,areFieldsSet=true,lenient=true,zone=GMT,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=199,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=30,SECOND=14,MILLISECOND=219,ZONE_OFFSET=0,DST_OFFSET=0]
now:Fri Jul 18 10:30:14 BRT 2014
as you can see, cal is 3 hours ahead of now... what am I doing wrong?
Thanks in advance.
[EDIT] Looks like TimeZone.setDefault(TimeZone.getTimeZone("UTC")); before the code above does the job...
This question has already been answered here
The System.out.println(cal_Two.getTime()) invocation returns a Date from getTime(). It is the Date which is getting converted to a string for println, and that conversion will use the default IST timezone in your case.
You'll need to explicitly use DateFormat.setTimeZone() to print the Date in the desired timezone.
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(timeZone);
System.out.println("Time zone: " + timeZone.getID());
System.out.println("default time zone: " + TimeZone.getDefault().getID());
System.out.println();
System.out.println("UTC: " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());
Edit To convert cal to date
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
System.out.println(year);
Date date = new Date(year - 1900, month, day); // is same as date = new Date();
Just build the Date object using the Cal values. Please let me know if that helps.
Try using a date formatter and set the time zone to UTC.
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

java - Start Date of Last Month [duplicate]

This question already has answers here:
How to get the first date and last date of the previous month? (Java)
(9 answers)
Closed 7 years ago.
I want to calculate the start date of the last month. I have referred this post and I have written the snippet as follows:
Calendar calendar = getCalendar(new Date());
calendar.set(Calendar.DAY_OF_MONTH-1,
calendar.getActualMinimum(DAY_OF_MONTH));
calendar = getTimeToBeginningOfDay(calendar);
return calendar.getTime();
With this I'm able to get the Date before the end of the last month. Help me get the start date of Any help would be appreciated.
You should never do math on field constants such as Calendar.DAY_OF_MONTH. What you want to do is:
Calendar calendar = getCalendar(new Date());
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar = getTimeToBeginningOfDay(calendar);
return calendar.getTime();
You may get what you expect with :
Calendar c = Calendar.getInstance();
// substract one month
c.add(Calendar.MONTH, -1);
// get to the lowest day of that month
c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
// lowest possible time in that day
c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
If you just want the first day of the previous month, then this should do it.
Calendar instance = Calendar.getInstance();
instance.add(Calendar.MONTH, -1);
instance.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(instance.getTime());
If you need to to clear out the time component, then use getTimeToBeginningOfDay() mentioned in the post to achieve this.

How to set a Java Date object's value to yesterday? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get yesterday's date using Date
What is an elegant way set to a Java Date object's value to yesterday?
With JodaTime
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minus(Period.days(1));
System.out.printf("Today is : %s, Yesterday : %s", today.toString("yyyy-MM-dd"), yesterday.toString("yyyy-MM-dd"));
Do you mean to go back 24 hours in time.
Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
or to go back one day at the time same time (this can be 23 or 25 hours depending on daylight savings)
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
These are not exactly the same due to daylight saving.
Convert the Date to a Calendar object and "roll" it back a single day. Something like this helper method take from here:
public static void addDays(Date d, int days)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DATE, days);
d.setTime(c.getTime().getTime());
}
For your specific case, just pass in days as -1 and you should be done. Just make sure you take into consideration the timezone/locale if doing extensive date specific manipulations.
you can try the follwing code:
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
As many people have already said use Calendar rather than date.
If you find you really want to use dates:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -24);
cal.getTime();//returns a Date object
Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.DAY_OF_MONTH, -1);
cal1.getTime();//returns a Date object
I hope this helps.
tomred
You can try the following example to set it to previous date.
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Today's date is " +dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));

Categories

Resources