I am trying to parse a String that looks like:
2015, 2, 31, 17, 0, 1
so i figured I'll use
SimpleDateFormat("yyyy, MM, dd, hh, mm, ss")
but it assumed the months are 1-based. In this case the month (2) is March. How can i tell SimpleDateFormat or any other class to parse with zero-based months?
Use Calendar:
String[] yourString = "2015, 2, 31, 17, 0, 1".split(",");
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, Integer.valueOf(yourString[0]));
c.set(Calendar.MONTH, Integer.valueOf(yourString[1]));
c.set(Calendar.DAY_OF_MONTH, Integer.valueOf(yourString[2]));
c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(yourString[3]));
c.set(Calendar.MINUTE, Integer.valueOf(yourString[4]));
c.set(Calendar.SECOND, Integer.valueOf(yourString[5]));
One solution i can see increase one month with
Date newDate = DateUtils.addMonths(new Date(), 1);
With calendar
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
By default months are 0 based index. see
Why is January month 0 in Java Calendar?
Why are months off by one with Java SimpleDateFormat?
Related
I want store a date into a Calendar Object, like this:
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, Calendar.JUNE);
cal.set(Calendar.DAY_OF_YEAR, 26);
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
All values are set correctly, except the month and if I call cal.getTime() it returns:
Thu Jan 26 10:00:00 CET 2017
What am I doing wrong?
When you use DAY_OF_YEAR you set the number day of the current year.
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#DAY_OF_YEAR
DAY_OF_YEAR Field number for get and set indicating the day number
within the current year.
This overrides all sensible configuration like month or year (to current year and month of the number day).
So instead of use DAY_OF_YEAR you may use DAY_OF_MONTH which seems is what you are looking for, this sets the day of the month you set before.
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#DAY_OF_MONTH
DAY_OF_MONTH Field number for get and set indicating the day of the month. This is a synonym for DATE. The first day of the month has value 1.
So the configuration you are looking for definetively seems it would be like next:
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, Calendar.JUNE);
cal.set(Calendar.DAY_OF_MONTH , 26);
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Then when you call getTime you will get:
Mon Jun 26 11:00:00 CEST 2017
As mentioned in "Why Java Calendar set(int year, int month, int date) not returning correct date?" this is an easy way to initialize a Calendar Object.
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.set(2017, Calendar.JUNE, 26, 9, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(cal.getTime());
Mon Jun 26 11:00:00 CEST 2017
Basically, I want my result is what I expected that print 2012-10-23. However, It is very wired.
Here is my codes:
SimpleDateFormat ft = new SimpleDateFormat("YYYY-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(2012, 10, 22);
cal.add(Calendar.DATE, 1);
Date startDate = new Date();
startDate = cal.getTime();
String date = ft.format(startDate).toString();
System.out.println(date);
I want to print 2012-10-23, but the result is 2012-11-23.
Can someone tell me why it adds 1 month automatically? Thank you.
Calendar class months starts from 0, not from 1, so when setting month as 10 you're not setting October but November.
Change your code to
cal.set(2012, 9, 22);
Or even better
cal.set(2012, Calendar.OCTOBER, 22);
More info:
java.util.Calendar
I want a javascript or java program should always give date 1st of current month.
Is there any tech?
You can use Calendar for Java
Date date = new Date(System.currentTimeMillis());
cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
Now you do what every you want to do with this Calendar object like to get the Day of the Week (Sat, Sun, .... )
int weekday = cal.get(Calendar.DAY_OF_WEEK);
And for JavaScript you can use:
var theFirst = new Date();
theFirst.setDate(1);
setDate sets the day of the month for the Date object (from 1 to 31). Then you can do whatever you want with theFirst, like get the day of the week.
Calendar ans = Calendar.getInstance();
ans.set(ans.get(Calendar.YEAR),
ans.get(Calendar.MONTH),
1,
0,
0,
0
);
System.out.println(ans.getTime());
I have a problem in java with sql date. I have the value of year-y, month-m,day-d I create new Date(y,m,d) but it says that this constructor is deprecated and it returns wrong date,
how can I correctly set date, having y,m,d ?
According to the API,
As of JDK version 1.1, replaced by
Calendar.set(year + 1900, month, date)
or GregorianCalendar(year + 1900,
month, date).
You should use public Date(long date) where date is the number of milliseconds since January 1, 1970, 00:00:00 GMT
In your case you can also use:
Date mydate = Date.valueOf("2011-05-04"); // yyyy-mm-dd
Here's how you can convert between the two:
new java.util.Date(sqlDate.getTime());
Calendar c = Calendar.getInstance();
c.set(year, month, date);
Date d = c.getTime();
Use Calendar.
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, d);
c.set(Calendar.MONTH, m);
c.set(Calendar.YEAR, y);
Then if you have to use dates, then you can do the following -
Date d = c.getTime();
Use the Calendar class:
Calendar cal = Calendar.getInstance();
cal.set(year, month, date);
java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());
I tried to do set date value to a PreparedStatement with default value but the value is sometimes returned as a JulianValue. For example (Assume spanBegin and spanEnd are null)
Calendar cal = new GregorianCalendar();
if (spanBegin == null) {
cal.set(0000, Calendar.JANUARY, 1);
spanBegin = cal.getTime();
}
if (spanEnd == null)
{
cal.set(9999, Calendar.DECEMBER, 31);
spanEnd = cal.getTime();
}
On line number 3, since the date January 1, 0000 is scoped by a Julian Calendar, the CDate becomes a Julian Calendar. However, the next Date even if it is in the year 9999, its CDate becomes a Julian Calendar still. I had to create another instance of Gregorian Calendar to fix the issue.
Calendar cal = new GregorianCalendar();
if (spanBegin == null) {
cal.set(0000, Calendar.JANUARY, 1);
spanBegin = cal.getTime();
}
Calendar cal = new GregorianCalendar();
if (spanEnd == null)
{
cal.set(9999, Calendar.DECEMBER, 31);
spanEnd = cal.getTime();
}
The question is, is the this an expected behavior or a bug on the date object? Actually using GregorianCalendar.getInstance() shows that the cdate is sometimes set to JulianCalendar.
There was no Gregorian Calendar until 1582. The Julian calendar was in use all over Europe, until minor problems started to appear caused by the fact the solar year is not exactly 365.25 days, but a little less than that. In order to fix things, pope Gregory XIII ordered to change the calendar to what we know today - every year that divides by 100 is not a leap year, unless it divides by 400. In October 1582 there was the transition - the day after 4 Oct was 15 Oct. This means that until October 1582, the Gregorian and Julian Calendars are the same. You can read more about it here
This is why dates prior to Oct 1582 are converted to use the Julian system. According to the API If you actually need to represent an historical event (which seems not to by the case here) you can do it only from 1st march, 4AD
What version of Java are you using and on what OS? Do you really need to store dates in the years 0 and 9999, or are you just using these as "negative infinity" and "positive infinity" values? How exactly do you see that the calendar is a Julian calendar?
I tried this:
Calendar cal = Calendar.getInstance();
cal.set(0, Calendar.JANUARY, 1);
Date d1 = cal.getTime();
cal.set(9999, Calendar.DECEMBER, 31);
Date d2 = cal.getTime();
System.out.println(d1);
System.out.println(d2);
Output (on Windows XP, using Sun Java 1.6.0_16):
Thu Jan 01 09:53:56 CET 1 java.util.Date
Tue Dec 31 09:53:56 CET 9999 java.util.Date
It changes the year 0 to the year 1. Changing the code to use a second Calendar object for the second date:
Calendar cal = Calendar.getInstance();
cal.set(0, Calendar.JANUARY, 1);
Date d1 = cal.getTime();
Calendar cal2 = Calendar.getInstance();
cal2.set(9999, Calendar.DECEMBER, 31);
Date d2 = cal2.getTime();
System.out.println(d1);
System.out.println(d2);
This does not change anything to the output or the content of the two Date objects.
Note: Beware that integer literals that start with a 0, such as 0000 in your code, will be interpreted as octal numbers by the Java compiler. That doesn't matter in this case because the number is 0, but you should not prepend integer literals with zeroes if you don't mean them as octal numbers.
Thhere is no year 0 in Julian calendar. It goes from 1 BC to 1 AD.