If date param is 2015-08-08, in my country (timezone = +7) the day of the month will be 8 – exactly what i want. But in my partner's country (timezone = -8) the day of the month is 7.
Can anyone tell me why?
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
Calendar#getInstance is using the default timezone, which is by default, your own. Any changes that you see are reflected based on your local timezone.
If you want to change that, you can pass in -Duser.timezone as a parameter to your program to ensure that it starts in a specific timezone.
Related
In the following snippet of code, I try to get the date of first day of current week, in a week system that starts from Saturday:
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.SATURDAY);
cal.set(Calendar.DAY_OF_WEEK,cal.getFirstDayOfWeek());
when I run this code here, the computed date is correctly previous Saturday (today is Tuesday by the way), but when I use this code in my Android program, date is set to next Saturday! Any idea why?
Try Calendar with the timezone or locale arguments. That way you should be able to make sure there isn't a different value for different locales / timezone.
Like this:
Calendar.getInstance(Locale.CANADA);
The Calendar API is confusing me. What I'm trying to do seems simple, but doesn't really appear to work for me.
I want to set the first day of the week to be Monday, rather than the default Sunday (I know it differs by Locale, but I want to enforce always Monday). On top of that, for a given date, I want to retrieve the day of the week it represents (e.g., 1 for Monday, 7 for Sunday).
Is there any way to do this with the default Calendar API? I've tried something like:
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(<some Date object that's a monday>);
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
It gives me a 2, instead of a 1. I suspect it's because it's giving me the value of Calendar.MONDAY, but I'm not entirely sure. Based on that suspicion, the following does work:
Calendar cal = Calendar.getInstance();
cal.setTime(<some Date object that's a monday>);
System.out.println(
(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) ?
7 : cal.get(Calendar.DAY_OF_WEEK) - 1);
... but I'd rather not have that if-statement. Is there a way to do what I want with the straight-up Calendar API?
Looking at the Calendar source it would appear that setFirstDayOfWeek really only impacts the WEEK_OF_MONTH and WEEK_OF_YEAR calculations. Regardless of what day your week starts on, MONDAY is still MONDAY, and in Calendar MONDAY has a value of 2.
Blockquote
The first day of the week is derived from the current locale. If you don't set the locale of the calendar (Calendar.getInstance(Locale), or new GregorianCalendar(Locale)), it will use the system's default. The system's default can be overridden by a JVM parameter:
public static void main(String[] args) {
Calendar c = new GregorianCalendar();
System.out.println(Locale.getDefault() + ": " + c.getFirstDayOfWeek());
}
This should show a different output with different JVM parameters for language/country:
-Duser.language=en -Duser.country=US -> en_US: 1 (Sunday)
-Duser.language=en -Duser.country=GB -> en_GB: 2 (Monday)
Don't forget that this could change other behavio(u)r too.
Blockquote
Detail can be found at following link
How to specify firstDayOfWeek for java.util.Calendar using a JVM argument
I have 2 questions:
How can I find what is the day of the week according to a specific date in JAVA ?
I want to find a difference between 2 times (each time include date and hour) in Java or PHP, someone can help me with that?
Thanx,
EDIT:
I still have a problem with that, I dont success to find the date of a specific date... i'm trying 2 ways, boths are not working.
1.
GregorianCalendar g = new GregorianCalendar(year, month, day, hour, min);
int dayOfWeek=g.DAY_OF_WEEK;
2.
Calendar cal = Calendar.getInstance();
cal.set(year, month, day, hour, min);
int dayOfWeek2 = cal.get(Calendar.DAY_OF_WEEK);
Someone cane give me another solution?
While not particularly great, the standard Java Calendar class should be sufficient for solving your first problem. You can set the time of a Calendar instance using a Date object, then access the DAY_OF_WEEK field. Something like this:
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
As for the second problem, it depends how you want the difference to be measured. If you just want the difference in milliseconds, you can simply call the getTime() method on each of your Date instances, and subtract one from the other. If you want it in terms of seconds, minutes, hours, days, etc you can simply do some simple arithmetic using that value.
Refer these links,
Find day of the week
to find the day of the week according to a specific date
try like below,
Calendar calendar=Calendar.getInstance();
calendar.setTime(specific_date);
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
Find the difference between two times
I'm attempting something and I'm not quite sure how to approach it. I have two user defined values.....a duration and a duration unit. At this point I will already have a start date, so I want to apply the duration and durationUnit somehow to startDate to get the endDate.
Duration is a decimal, durationUnit a HardCode/AbstractCode value and startDate is a Date.
So if the startDate is 17/12/2010......and the user enters the following;
Duration: 3
Duration Unit: Months
I want the endDate to then be calculated as 17/03/2011. Any idea on how I could do this? The duration unit could be Days, Months or Years.
Thanks in advance!
Have a look at joda-time. It's a superb replacement for Date/Calendar. You can do things like:
DateTime newDate = startDate.plusDays(days);
.plusYears(years);
Period toAdd = periodFormatter.parse(inputString);
DateTime newDate = startDate.plus(toAdd);
You can map the Day/Month/Year to the appropriate Calendar DAY, MONTH or YEAR. Then use calendar.add().
If by decimal you mean a double then you will probably have to do some processing to turn parts of a value into appropriate values (such as changing .5 DAY to 12 hours).
DateFormatter is also of use if you want a date to be represented in different styles.
Here, for the sake of brevity, the methods are invoked on classes which you should instatiate first whether using a factory or a constructor. The following statements are only for grasping the idea. Consult JavaDoc API for further details.
Calendar.set(...) //(to set year, month and day).
Calendar.add(...)//(to add a value on specific unit (month, day or year))
Calendar.getInstance() //(return Date object)
DateFormat.format(..) //(return a String representing a style used at instantiation of //DateFormat)
Haven't you tried using the Date type
Date myDate = new Date();
myDate.setMonth();
myDate.setDay();
I haven't use it myself, so i can really, tell you i f it really works, but you shoul give it a try
Others said it already, here a more concrete sample using java.util.Calendar:
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
if(durationUnit.equals("day")){
cal.add(Calendar.DATE, duration);
} else if(durationUnit.equals("month")){
cal.add(Calendar.MONTH, duration);
} else if(durationUnit.equals("year")){
cal.add(Calendar.YEAR, duration);
}
Date endDate = cal.getTime();
I am doing an assignment and it involves using the GregorianCalendar. The specs say I need to use setLenient(false); how do I do this? I also need to set a constant date (1/1/2009) so that the first day of my program is always that.
It also says to access the day, month and year through this:
get(1) //returns the year
get(2) // returns the month
get(5) /// returns the day
To add n days to the date, call the add method with a field number of 5: add(5, n);
To subtract: add(5, -n);
Can someone please explain what that means and how to implement it?
Start by visiting the API docs here. These docs explain exactly what methods are available in a class in Java.
To get a Calendar for instance you can:
Calendar c = Calendar.getInstance();
You will see in the docs that there are actually a number of ways to get a Calendar and the default is a GregorianCalendar.
Once you have the Calendar object then you can call any method passing the necessary parameters. For example,
c.setLenient(true);
To use the get methods you have to specify the field that you wish to get.
int month = c.get(Calendar.MONTH);
and so on.
Create an instance of Calendar and call setLenient on it.
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
int month = cal.get(Calendar.MONTH);
UPDATE:
And since you only mentioned SimpleDateFormat in your comment, here's an example for it as well:
Date today = cal.getTime();
DateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd");
System.out.println(formatter.format(today));
Java Almanac is a good source for simple code snippet examples like these.
To create a GregorianCalendar instance:
Calendar cal = new GregorianCalendar();
cal.setLenient(false);
References:
GregorianCalendar
setLenient (inherited from Calendar)