date 1 « Calendar « Java Data Type Q&A

1. How to sanity check a date in java

I find it curious that the most obvious way to create Date objects in Java has been deprecated and appears to have been "substituted" with not so obvious to use lenient ...

2. How do I discover the Quarter of a given Date?

Given a java.util.Date object how do I go about finding what Quarter it's in? Assuming Q1 = Jan Feb Mar, Q2 = Apr, May, Jun, etc.

3. Date arithmetic in Java

My program is about generating (producing) a Kurosawa and making the customers produce it. Every time we generate a Kurosawa, we have to print its id, its production date and expiration date, ...

4. calendar methods

apart from the inbuilt functions, can we use any simple formulas to calculate the start day of the month given month and year as inputs??

5. How do I calculate someone's age in Java?

I want to return an age in years as an int in a Java method. What I have now is the following where getBirthDate() returns a Date object (with the birth date ...

6. Java Calendar.set not giving correct result

I can't see where I'm going wrong with the code below, its probably something obvious but I'm too blind to see it at this stage. I'm passing a date of ...

7. Java Date vs Calendar

Could someone please advise the current "best practice" around Date and Calendar types. When writing new code, is it best to always favour Calendar over Date, or are there circumstances where Date ...

8. JSpinner Date Editor in Buddhist Calendar

Is there a way to use JSpinner.DateEditor with a Buddhist Calendar? When I change my locale to "th", "TH" and recreate my calendars they are indeed Buddhist Calendars. However, ...

9. Date Picker / calendar UI Component with draggable date ranges

I'm looking for a datepicker control (HTML + JavaScript / Flex) that allows selecting a range of dates by dragging along the calendar dates (just like in Google Calendar or Outlook) It ...

10. Sum two dates in Java

How can I add two dates in Java? Example: The sum of "2010-01-14 19:16:17" "0000-10-03 01:10:05"
would result in "2010-11-17 20:26:22". I know how to do it using Calendar and adding field by ...

11. How check if today is sunday with Java Calendar

I wrote few lines of code which doesn't work correctly. Why? Could sb explain me?

    Calendar date = Calendar.getInstance();

    date.set(2010, 03, 7);


12. Mayan calendar in Java

How i can use the maya calendar in Java?

13. Date object to Calendar [Java]

I have a class Movie in it i have a start Date, a duration and a stop Date. Start and stop Date are Date Objects (private Date startDate ...) (It's an assignment so i ...

14. Java Date sorting method?

I have an String array of dates in the format ex:'2010-05-04 11:26:46 +0530'. How can I check whether a particular date in the array is > today? thanks

15. Set Date in a single line

According to the java API, the constructor Date(year, month, day) is depreciated. I know that I can replace it with the following code:

Calendar myCal = Calendar.getInstance();
myCal.set(Calendar.YEAR, theYear);
myCal.set(Calendar.MONTH, theMonth);
myCal.set(Calendar.DAY_OF_MONTH, theDay);
Date theDate = ...

16. Java: How to check if a date Object equals yesterday?

Right now I am using this code

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) - 1, 12, 0, 0); //Sets Calendar to "yeserday, 12am"
if(sdf.format(getDateFromLine(line)).equals(sdf.format(cal.getTime())))      ...

17. Calendar formatting issues

We're searching for information on how to format instances of java.util.Calendar and more general information and coding hints regarding transition from using java.util.Date to java.util.Calendar. best, phil

18. How to calculate a Date for a 00:00:00 a.m. of a today?

I need to calculate a java.util.Date for a beginning of a today day (00:00:00 a.m. of a today day). Does someone know something better than resetting fields of java.util.Calendar:

Calendar cal = ...

19. Java: Iterating through start and end dates, best practice?

What is the best practice to iterate through a start and end date?

20. What exists in terms of a Java 'timeline' library?

I am interested in any library which can do the following: I have a time range: let's say 1995 to 2010 which I can classify in some way, let's say as "The ...

21. Parsing date from Calendar in Java

I am having following function

public static Date parseDate(String date, String format) throws ParseException
         SimpleDateFormat formatter = new SimpleDateFormat(format);

22. Is there a good way to get the date of the coming Wednesday?

Is there a good way to get the date of the coming Wednesday? That is, if today is Tuesday, I want to get the date of Wednesday in this week; if today ...

23. I want to calculate Saturday's and Sunday's in a date range

I want to calculate Saturday's and Sunday's in a date range? How can i ?

24. Create from a List a List> that has all Dates which are subsequently put in Lists

I hope you understood me. Hard to describe in english for me. So I have a List of sorted Date Objects. Now I want to put all Dates that are in a ...

25. Java Date getDate() deprecated, refactored to use calendar but looks ugly

Eclipse is warning that I'm using a deprecated method:

eventDay = event.getEvent_s_date().getDate();
So I rewrote it as
eventDay = DateUtil.toCalendar(event.getEvent_s_date()).get(Calendar.DATE);
It seems to work but it looks ugly. My question is did I refactor ...

26. How make a list of all calendar dates in 2009 and 2010 that fall on Monday to Thursday?

I need to loop backwards and make a list of all calendar dates in 2009 and 2010 that fall on Monday - Thursday of each week and record them as a ...

27. Java Calendar problem: why are these two Dates not equal?


public class testing {
    public static void main(String a[]) throws Exception{
        Date d1=new Date();

28. Is this a date in February or in March? cal.set(2010, 1, 10)

I have set this calendar in my app:

Calendar cal = Calendar.getInstance();
cal.set(2010, 1, 10); 
I'm using this SimpleDateFormat to get the month as a word:
SimpleDateFormat formatMonth = new SimpleDateFormat("MM");
In one class it ...

29. Date and calendar java

class Employee
private Date doj;

public Employee (Date doj)
public Date getDoj()
return doj;

class TestEmployeeSort
public static List<Employee> getEmployees()
  List<Employee> col=new ArrayList<Employee>();
  col.add(new Employee(new Date(1986,21,22));
In the above code i have used Date to set ...

30. calendar api to display date alone

    import java.util.Calendar;

    public class Employee {
        private Calendar doj; 

31. calendar api to set many dates

    import java.util.Calendar;
    import java.util.Date;
    public class Employee {
        private Calendar doj; 

32. Bug in Java Calendar / Date for 2nd October 2010?

I'm not sure what I'm doing wrong, but I've got a piece of code which calculates the number of days between two dates, and which looks something like the following:

final Calendar ...

33. how can explain this java code about calendar

I want to understand this code. I know this code gives me the calendar when I enter the year, month and day. Then program shows the calendar of that month, additionally ...

34. Unique int from Date

This is general programming, but I'm working in Java. Given a date dd-mm-yy or dd-mm-yyyy (e.g. 13-01-2011) I want to convert this into a unique number such that any two dates have ...

35. JodaTime: Interval of calendar dates

Possible Duplicate:
LocalDate interval in Joda-time
How can I create an interval of calender dates in JodaTime (or using some other framework)? I need a class that ...

36. Java Calendar object retrieving wrong date?

 * Gets an instance of GMT Calendar.
 * @return a new <code>Calendar</code> object
public static Calendar getGMTCalendar()
    return Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);

 * @return The month (eg. Calenday.MAY) ...

37. Help with java calendar logic

I am working with the Java Calendar class to do the following:

  1. Set a start date
  2. Set an end date
  3. Any date within that range is a "valid" date
I have this somewhat working, and ...

38. Getting a Specific Date Range

I need to get a date range starting last Wednesday to yesterday. This program is run on every Wednesday. Is it the good way to do it? What if the program ...

39. Debug simple java code related to Calendar Date GMT

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Test {

    public static void main(String[] args) throws ParseException {

        Calendar dateFromNet = strToCal("11-MAR-2004", ...

40. How to get date of last Friday from specified date?

How can I find out the date of last (previous) "Friday" or any other day from a specified date?

public getDateOnDay(Date date, String dayName) {
    // ?

41. What's the right way to create a date in Java?

I get confused by the Java API for the Date class. Everything seems to be deprecated and links to the Calendar class. So I started using the Calendar objects to do ...

42. Using Calendar, how can i discover relative date?

Possible Duplicate:
How to get date of last Friday from specified date?
Using java.util.Calendar (or any other library), is there a way i can find out ...

43. Whether it is correct to take 01/01/0001 date as Monday?

I am writing a program to display the Day for the given date. (E.g. Thursday for 1/1/1970.) While attempting that I have some problems. This is my program.

 The noOfDays variable will ...

44. Date operations in Java?

I want to get the year/month/day values of a Date object, but the docs say the getYear(), etc. methods are deprecated. It suggests to use Calendar instead, ...

45. Java - get differences between given fortnight and current fortnight

For example:
Data Given :

  1. Year-2010, Month-2, Fortnight-1

  2. Current date
How do I get the ...

46. How to reset an XMLGregorianCalendar to new values

I'm going to be doing some work with dates millions of times per day. I've created an XMLGregorianCalendar to handle the dates from an XML feed as such:

XMLGregorianCalendar xCalEst = ...

47. Why does Java's Date.getYear() return 111 instead of 2011?

I am having a bit of trouble parsing a string date to a Date object. I use a DateFormat to parse the string, and when I print the value of the ...

48. Find all Saturday and Sunday from given date range

I want to take all Saturday and Sunday from given date range... my inputs are Start Date : 01/01/2011 End Date : 01/01/2012 now search date which is in between given start date and ...

49. Java: how to use Date / Calendar

I'm making a small project for my university which is supposed to enroll students into different courses. I do have one issue though, each course has start/end enrollment day. How can ...

50. Java calendar get the current date, without hours, minutesm seconds and milliseconds, in milliseconds

I would like to get the current date in milliseconds with only year, month and date. But when I use this code:

Calendar cal = Calendar.getInstance();
currentDate = cal.getTimeInMillis();
I still get the time ...

51. Does set of java Calendar always go to future date?

In Java Calendar exists method set. E.g. now is 01/11/2011. If we make like:

Calendar now = new GregorianCalendar();

Calendar will be set to nearest Friday, but for date more than now. In docs ...

52. Unexpected behavior with Calendar class of Java

I have following code to get different parts of current system Date (10-11-2011 for this case).

Calendar now = Calendar.getInstance();
String dt = ""+now.get(now.DATE)+"-"+now.get(now.MONTH)+"-"+now.get(now.YEAR);
Here, DATE and YEAR fields are giving values as expected ...

53. A scenario about the calendar in Java in which explicitly setting a date leads to some diffrent result

Let's look at the following code snippet in Java.

package demo;

import java.util.Calendar;

final public class Main
    public static void main(String[] args)

54. Problem in Incrementing Calendar Date ?

HI friends my context is: user will give some dates example 20/05/2008 to 04/07/2008 (dd/mm/yyyy). i need to insert every date into database between those dates. my code is: st = new StringTokenizer("05/20/2008","/"); Calendar c1 = Calendar.getInstance(); c1.set(Calendar.MONTH, Integer.parseInt(st.nextToken())); c1.set(Calendar.DATE, Integer.parseInt(st.nextToken())); c1.set(Calendar.YEAR, Integer.parseInt(st.nextToken())); st = new StringTokenizer("04/07/2008","/"); Calendar c2 = Calendar.getInstance(); c2.set(Calendar.MONTH, Integer.parseInt(st.nextToken())); c2.set(Calendar.DATE, Integer.parseInt(st.nextToken())); c2.set(Calendar.YEAR, Integer.parseInt(st.nextToken())); while(c2.equals(c2)) { c1.add(Calendar.MONTH, -1); ...

55. Format Date using Calendar

If all you want to do is format the current time, there is no reason to use the Calendar class at all. Your code example using just the Date object is fine. If you are trying to format a Calendar object containing an arbitrary time, then you can use the Calendar class's getTime() method to "convert" the Calendar into a Date ...

56. generating calendar using given date

hi, the application shud be like...i have to enter a date of a month and the day of the week, and the corresponding year's calendar has to be generated. suppose if i enter 31 march, thursday. then it has to generate the calendar of this year relative to the date and day i have given and leap year consideration need not ...

58. Understanding Calendar and Date...

I'm working on a scheduling application and am trying to understand how time, dates, etc, are normally represented in Java programs. In my application I'm typically scheduling lots of "short" duration things (a few minutes in duration) over the course of 1-2 days. Time is quantized to second level precision for the application. Typically I envision storing an eventDate and a ...

59. Difference in Date Calendar classes

60. Date into Calendar? Howto?

61. date or calendar object creation

62. calendar dates

63. Date, Calendar, DateFormat

Not sure about the significance of that date, but's really not that important. It's just a relative marker in time. Time is measured as a displacement relative to that date. Since the underlying data behind a Date() is a long, that will let you address +/- 292 Million years from Jan 1 1970, to a precision of a millisecond. So in ...

64. Calendar class and getting the correct date

I'm a bit confused and hoping someone can help me. I need my program to grab the current time from the OS and then compare that to the time I gave it. The problem I'm having is that it's only grabbing the time once. For example, if I start the program at 1410 then the time will ALWAYS be 1410 and ...

65. Calendar Class and dates???

I was wondering if anyone knows where the field values come from, I mean for the time zones etc.? Is it from the computer settings? Also when you roll an instance what is the persistance? can the rolled values be used for other components, and can you "re-roll" the instance after you've changed it once already? I'm fresh and 'green' and ...

66. Date, DateFormat, Calendar - resource request

Can anyone recommend a good place to read up on Date, DateFormat, Calendar? I have a very basic understanding on how to deal with dates and I would like to learn more. The books I have touch on it but the examples don't do a great job and I end up struggling on how to malipulate dates. I do not have ...

67. how to make a calendar using Date, Calender and Data Class

presumably you want the output lined up with a S M T W T F S header. if so, create a calendar object, determine the day of the week for the 1st of the selected month/year. Print the days from the day of the week to the getAbsoluteMaximum() of the number bof days in the month. when you get to a ...

68. using Date and Calendar classes

Hello, I am a beginner in JSP and am working on this exercise: Using Date and Calendar objects, put together a JSP page that displays the number of days remaining until your next birthday. (Hint: the DAY_OF_YEAR field will be useful here.) I think I am getting the wrong result as the no. of days that I get when I use ...

69. Calendar shows incorrect date

Hi, I have a simple code here Calendar currentDate = Calendar.getInstance(); System.out.println("currentDate : " + currentDate.get(Calendar.DAY_OF_MONTH) + "/" + currentDate.get(Calendar.MONTH) + "/" + currentDate.get(Calendar.YEAR)); I executed this on the 7th of September 2007...i.e. 07/09/2007. The output is currentDate : 7/8/2007 Am I missing something fundamental here? Please help and thanks in advance.

70. Date and Calendar

I want to Calculate current date + 30 days. If calculated date = 1st of a month then use this date. If it is not the 1st of a month, then use the 1st of the following month. Exampleif today is 15 April 2007, then expiry date will be 1 June 2007. If today is 2 May 2007, then expiry date ...

71. Calendar returns wrong date?

Hey everyone, Either I have a bug or Sun is playing an April Fools joke on me. Is there any possible way that Calendar.getInstance() would return a date of 3/1/2008 instead of 4/1/2008?? Maybe one of you can find the error in my code. I am tryign to set 4 text boxes. One set had today's date and time the other ...

75. Grouse about Calendar, DateFormat, Date

76. How to parse a user-entered Date into a Date or Calendar object

The standard DateFormat (and subclasses) handle parsing of date strings for well defined values using a mask. Is there a more general solution? Something that does not force the user to enter dates in one and only one format? I'd like to let the user enter what they want, say 02/14/10 or Jan 14 2010 or 15-feb-10 It would be cool, ...

77. Java Calendar Dates

Hi all, I am currently building a payroll software code and stuck at a point, I would do my best to explain and get any help I have a pay period Start date and End Date this can be any period When I enter the period as (in mm/dd/yyyy format) Start date "04/08/2010" End Date "05/08/2010" then I add 1 month ...

78. Java Calendar Dates

Hi all, I am currently building a payroll software code and stuck at a point, I would do my best to explain and get any help I have a pay period Start date and End Date this can be any period When I enter the period as (in mm/dd/yyyy format) Start date "04/08/2010" End Date "05/08/2010" then I add 1 month ...

79. timer task not working with calendar date object

The idea is to have the timer task execute once every 12 hours ... specifically at 6am and 6pm. If I use a generic date object which is set to the current time ... it works. However when I use Calendar to create a new date object it runs constant non stop and I can't figure it out. Here is my ...

80. Calendar function to set date

class Employee { private Date doj; public Employee (Date doj) { this.doj=doj; } public Date getDoj() { return doj; } } class TestEmployeeSort { public static List getEmployees() { List col=new ArrayList(); col.add(new Employee(new Date(1986,21,22)); } } In the above code i have used Date to set a date. I want to know how to use calendar function to do this. ...

81. setting calendar to an arbitrary date

This code does not work because there is no set method for Calendar and setTime errs because it's a non-static method. Is setting up GregorianCalendar the simplest way to set Calendar given an input string "M/d/yy"? import java.util.*; import java.text.*; public class ScriptTest { public void main (String[] args ){ String LINE_END = System.getProperty("line.separator"); String strDate = "11/11/11"; int lengthOfStay = ...

82. Display date format by calendar entry

83. Calendar date problem

01-17-2010 04:02 AM #1 groobelar Member Join Date Jan 2010 Posts 1 Rep Power 0 Calendar date problem Test The calendar shows, from left to right, Monday-Sunday. For each day, the date turns green from monday-saturday (for example, try to set your computer day to 15 January). However, no green color is there when the day is a Sunday ...

84. Calendar object? - Getting date from user

I'm going to go ahead and edit my OP for the last part of my task. I need to compare a user-prompted date, with the system's date using currentTimeMillis. If the prompted date is before the system date I will print X and if it is after I will print Y. Java Code: import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class ...

86. get number of weeks from a given date using calendar function

Now read your initial question - does it state the same? No it does not. Please be precise because none of the forum users are mind readers. There are never five full weeks in a month. But maybe the full weeks are not what you want? What do you want exactly? Regards,

88. Date&Calendar Tutorial

89. Date to calendar

(Few methods of Date class in java 1.6 are deprecated so I m using calendar class.) I have a user defined method which calls the constructor, and gets the current date by calling the constructor but super(year, month, day) for Date is deprecated. Please can you suggest me the replacement for the constructor using calendar class.

92. Date and Calendar issues!!!

paul_b wrote: Hi, I am struggling to get the GMT using Calendar and Date classes!!!! I have included the code below to illustrate my point I need to have 2 times and calculate the difference between them. One being the UK current time(with DST) and the other being GMT. I need to be also calculate differences between timezones. And I am ...

93. Calendar behaves diffrently than Date

I have no idea what you're rambling on about now. You created a Date object representing one minute past midnight CET on the 20th January 2010. Or, with the suggested modification, midnight on the same day. You created a Calendar object representing midnight UTC on the 20th January 2010. You then converted the latter to a Date instance representing the same ...

94. Calendar Date is wrong

Works fine for me. As far as I know I'm also Eastern Standard Time. I'm using JDK6 on XP. Display the value of the calendar at each step to see where it goes wrong. And post you 10 line demo code so other people can test to see what results they get.

95. Where do Date and Calendar get the current date?

Maybe because Java has a different notion of "local time zone" than whatever you use to check the time. Try checking the local timezone that Java thinks is in effect and compare this with whatever the OS provides. A common case seems to be that Java has more up-to-date timezone data than the OS and Java is actually true. And: you ...

96. Calendar.DATE returning wrong value?

I put in some TEST variables to check what my markers were being set to (all correct except the DATE for yesterday's markers). Can someone please explain why Calendar.DATE is returning a value other than today's date, or what it is I should be doing? Thank you very much for your help. Edited by: jpjavajava on Apr 2, 2009 5:37 AM ...

97. Calendar and Date objects

I've got an application that has many Date objects. We frequently have to do comparisons to see if two Date objects represent the same day. I know that I can use a SiimpleDateFormatter to make a String from a Date. I can compare two of these Strings and determine if they are on the same day. I also know that I ...

98. Calendar returns incorrect date

99. Unable to parse date from Calendar

In my application the DOB is chosen from a calendar component.The date retrieved of the format Fri Dec 12 00:00:00 IST 2008.How will i change the format to dd/MM/yyyy and pass the same to a function as date object itself.I tried using SimpleDateFormat and parsed the output.But i got parse Exception try { SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy"); Date date = (Date)formatter.parse(fromDate); }catch ...

100. Date and Calendar class