why do I get wrong month when parsing with java calendar - java

Date fakeDate = sdf.parse("15/07/2013 11:00 AM");
Calendar calendar = Calendar.getInstance()
calendar.setTime(fakeDate);
int currentMonth = calendar.get(Calendar.MONTH);
I get currentMonth == 6 instead of 7.
why is that?

Because Calendar.MONTH is ZERO based. Why?
Check the docs: (always)
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.

As the doc says - 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.
So try something like this
int currentMonth = calendar.get(Calendar.MONTH)+1;
Because
calendar.get(Calendar.MONTH) shall give you (currentMonthValue-1) as the value of january starts with 0

It should be
int currentMonth = calendar.get(Calendar.MONTH)+1;

Related

get number of the week in the year where first day of the week is Sunday

I'm trying to get the number of the week for a date , In my country the week begins on Sunday so the week number of 6/5/2016 is 23 but it returning 22 because the ISO week in JAVA starts from Monday , I have used the following methods but it's not working
mCalendar = Calendar.getInstance();
int weekNum = mCalendar.get(Calendar.WEEK_OF_YEAR); //returns 22 I need 23
// I have tried the following method but it has no effect
mCalendar.setFirstDayOfWeek(Calendar.SUNDAY);
note that I can't use the Time Class I can only use Java 7
Java 8 version full answer:
public<T extends Temporal> long getWeekNumber(T tObj) {
DayOfWeek firstDayOfWeek = DayOfWeek.SUNDAY; // set your
Temporal firstDayOfThisYear = tObj.with(TemporalAdjusters.firstDayOfYear());
Temporal firstDayOfWeekInMonth = firstDayOfThisYear
.with(TemporalAdjusters.dayOfWeekInMonth(1, firstDayOfWeek));
return ChronoUnit.WEEKS.between(tObj, firstDayOfWeekInMonth);
}
The parameter can be any Temporal type, even the Temporal itself.
I don't know from where you are but Java has an awesome Calendar which allows the following:
Calendar calendar = Calendar.getInstance(Locale.TRADITIONAL_CHINESE);
int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR);
System.out.println("Number of week: " + weekNumber);
// produces 24
Calendar calendar = Calendar.getInstance(Locale.UK);
int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR);
System.out.println("Number of week: " + weekNumber);
// produces 22
You could use the locale constants to specify your location and i think you will get the right number of weeks.
Edit:
Now I see the failure in your code. Please note that Java works from the top to the button of your code:
Calendar calendar = Calendar.getInstance();
// First set the first day of the week ...
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
// ... and than you could ask the calendar for the week
int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR);
// will produce 23
System.out.println("Number of week: " + weekNumber);
I don't know where you are from.
I'm french and that works perfectly this way :
Calendar mCalendar = Calendar.getInstance(Locale.FRANCE);
mCalendar.setFirstDayOfWeek(Calendar.SUNDAY);
int weekNum = mCalendar.get(Calendar.WEEK_OF_YEAR);
System.out.println(weekNum); // --> 23
Get the current moment.See this Question: How to initialize a variable of type date in Java?
Determine the first Sunday of the year.Handled thoroughly in this question: How to get all the Sunday's of a year
Count weeks between that first Sunday and current moment.See the Question: Get the number of weeks between two Dates
I've just figured out how to change it
you need to set up two things
1-first day of the week
2-the minimal day of week
setFirstDayOfWeek(Calendar.SUNDAY);
setMinimalDaysInFirstWeek(7);
this will tell the calendar to make the fist day is sunday and with 7 days minimal week

How to Extrapolate information by date in Java

I have this object Date in this format : 2014-05-20 18:17:26.337
I try to do this:
Calendar c = Calendar.getInstance();
c.setTime(myDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
int Month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
But the information are wrong..
How I can resolve it?
This is the expected value, please read the javadoc carefully:
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#MONTH
For 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.
For day of week:
public static final int DAY_OF_WEEK
Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
From java doc:
Blockquote
int java.util.Calendar.MONTH = 2 [0x2]
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.
See Also: JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
OCTOBER NOVEMBER DECEMBER UNDECIMBER
You need to read Java doc first
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.
So you will get month as 4
int month = c.get(Calendar.MONTH);

Im trying to calculate the number off days between 2 date objects [duplicate]

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 8 years ago.
public static int countWeeks() {
// setting dates
Calendar calStart = GregorianCalendar.getInstance();
calStart.set(2014, 8, 30);
Date dateStart = calStart.getTime();
Date dateEnd = new Date();
// count days and weeks
int diffInDays = Days.daysBetween(new DateTime(dateStart), new DateTime(dateEnd)).getDays(); // int weekNumber = (int) diffInDays / 7;
return weekNumber;
}
I'm trying to calculate the number of days and weeks between today and last week but I always get -3 as weekNumber. I have no idea what i'm doing wrong.
Thanks in advance.
First, I will assume that
int weekNumber = (int) diffInDays / 7;
is not commented since otherwise you would get a compilation error.
Now, as explained in my comment, by doing
calStart.set(2014, 8, 30);
You are setting the date at the end of setember, not of august. So, it is 3 weeks ahead of now, so you get a -3. Use the Calendar constants.
calStart.set(2014, Calendar.AUGUST, 30);
You set the startdate to Sep. 30th because te month is zero bases!
See the documentation from java.util.Calendar:
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. See Also:
You are getting -3 as the weeknumber since you have commented that out so it showing some random value. Also note that 8 shows the September month not Aug since months are 0 based.
So if you are aiming at August month then you may try this:
calStart.set(2014, 7, 30);
^^-- This represents August month
If you are on Java 8 you can use the Java time API:
LocalDate start = LocalDate.of(2014, 8, 30);
LocalDate end = LocalDate.now();
long days = ChronoUnit.DAYS.between(start, end);
return (int) days / 7;

JCalendar save date to different variables [duplicate]

I have a 3D array that contains 38 years, 12 months, and 31 entries for each month (regardless of how many days in that month). Like so: array[38][12][31]. I also have a JCalendar that is doing nothing now except looking pretty, and the JCalendar has a button underneath. How would I make it so that I can select a date in the calendar, then press the button and it returns the element of my array that would correspond to that date?
Something like
if(buttonPressed){
year = chosenYear - 1975;
month = chosenMonth;
day = chosenDay;
System.out.print(array[year][month][day]);
}
thanks guys.
You can get the selected Date in a PropertyChangeListener, as shown here. Once you have the date, you can get the year, month and day from a Calendar:
Calendar c = Calendar.getInstance();
c.setTime(date);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH);
Calendar.MONTH is already zero-based, but Calendar.DAY_OF_MONTH is not; and you'll need to adjust the year to your baseline.

The number of months in a Calendar is not constant?

From http://www.coderanch.com/t/381676/java/java/number-months-between-two-given, one post mentioned:
public static int monthsBetween(Date minuend, Date subtrahend)
{
Calendar cal = Calendar.getInstance();
// default will be Gregorian in US Locales
cal.setTime(minuend);
int minuendMonth = cal.get(Calendar.MONTH);
int minuendYear = cal.get(Calendar.YEAR);
cal.setTime(subtrahend);
int subtrahendMonth = cal.get(Calendar.MONTH);
int subtrahendYear = cal.get(Calendar.YEAR);
// the following will work okay for Gregorian but will not
// work correctly in a Calendar where the number of months
// in a year is not constant
return ((minuendYear - subtrahendYear) * cal.getMaximum(Calendar.MONTH)) +
(minuendMonth - subtrahendMonth);
}
Is it true that the number of months in a Calendar is not constant? And why?
Yes. In the hebrew calendar, there are several years with 13 months (7 out of 19 to be exact).
It is funny that, month comes from moon. A lunar calendar usually sync days with moon phases, so that, for example, day 15 of any month is always a full moon day.
The problem is a solar year is not exactly 12 moon cycles. So a lunar calendar must have "leap months".

Categories

Resources