File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Calendar Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar Class" Watch "Calendar Class" New topic
Author

Calendar Class

Donna Meyer
Greenhorn

Joined: Sep 12, 2000
Posts: 21
Hi!
This is my first post to this forum and I'm hoping someone will be able to help me. I'm struggling with the DateFormat & Calendar classes. I don't know if I've developed a mental block but I'm getting pretty confused. I want to take a string value and create a Date in the format 00/00/00. Then derive the day of the year and concatenate it with a 2 digit year. For example, 00/00/00 to 013201. Has anyone done this before? It doesn't seem like it should be this difficult. I can always resort to indexOf and parse the values, but I was under the impression the Calendar could do this for you.
Thanks much for any help offered!
Donna
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Donna,
Below is a little program that uses the DateFormat and Calendar classes to read a String and tell what day of the year it represents:

Unfortunately, I didn't understand your entire question. In particular, what do you mean by "00/00/00 to 013201". It doesn't seem like either of these items is a valid date. I would like to help you. Could you maybe reword your question so that the goal is a little more clear?
Stephanie
Donna Meyer
Greenhorn

Joined: Sep 12, 2000
Posts: 21
Thanks for your response! I'll try to rephrase my question. I've been in a "tissy" over this problem. I have an applet with a textfield that the user will key in a date in the format of dd/mm/yy. I need to return a zero padded day of year and year ... such as 024500 for date 09/01/00. I can't seem to make an object for Calendar to extract the data. I'm about to resort to the old fashioned parsing and case statements to achieve what I need. I was wondering if anyone else has done this and if so how. Or is it me and a "brain-block".
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Originally posted by Stephanie Grasson:
[B]Donna,
Is this more what you want?

Stephanie[/B]

Donna Meyer
Greenhorn

Joined: Sep 12, 2000
Posts: 21
Stephanie,
I really appreciate your time on this! I was trying to use the Calendar class to allow for leap years calculation. I'm using both the SimpleDateFormat to get a 2 digit year and used Calendar to get the day of year as Calendar was only returning a 4 digit yr. However, if a wrong date is entered such as 02/40/00, it will still calculate it. It also isn't allowing for a leap year calculation. Any thoughts?
[This message has been edited by Donna Meyer (edited September 13, 2000).]
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Donna,
The code I posted works on my machine, returning the appropriate DAY_OF_YEAR in the format you requested. If you enter "09/01/00" in the upper TextField and press the Convert button you get "024500" in the lower text field. I have tested it with a number of cases. I am using JDK1.2.2 on a WindowsNT 4.0 workstation.
If you want to post your code, I would be happy to look at it. What values are you getting as return for DAY_OF_YEAR? Do you get errors or warnings, or simply garbage values? What version of the JDK are you using? Have you checked the bug list for your version with respect to the DateFormat and SimpleDateFormat classes? (I ask because I tried using the 1.3 version for awhile, but found it still has many bugs and went back to the 1.2.2).
Also, you mention that if a "wrong" date is entered (such as "2/40/00"), it still calculates it. In the documentation for the Date class it says that:
"In all cases, arguments given to methods for these purposes need not fall within the indicated ranges; for example, a date may
be specified as January 32 and is interpreted as meaning February 1." Maybe something similar is happening for you.
Anyway, I'm sorry you're still having trouble. I will wait for your next post.
Stephanie
[This message has been edited by Stephanie Grasson (edited September 13, 2000).]
Donna Meyer
Greenhorn

Joined: Sep 12, 2000
Posts: 21
Stephanie,
Sorry I edited my post when I realized that it was misleading as the code is returning something. I don't know if I'm under a wrong assumption about what this class can & can't do and maybe I have to write something to handle this. Any thoughts?
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Donna,
It is difficult to trouble-shoot without seeing what your code is doing. If your code is proprietary, could you post just the portion that is giving you problems? I'm not sure what to suggest without seeing the actual problem.
Stephanie
Donna Meyer
Greenhorn

Joined: Sep 12, 2000
Posts: 21
Sure ... I don't mind posting the code. See below:
public void convert() {
String conv_dt = null;
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
int dayOfYear = 0;
String yr = null;
try {
Date dt = df.parse(datefield.getText());
Calendar c = df.getCalendar();
c.setTime(dt);
c.setLenient(false);
dayOfYear = c.get(Calendar.DAY_OF_YEAR);
SimpleDateFormat sdf = new SimpleDateFormat("yy");
yr = sdf.format(dt);
}
catch (ParseException pe) {
converted_date.setText(pe.getMessage());
}
if (dayOfYear < 100)
converted_date.setText("00" + Integer.toString(dayOfYear) + yr);
else
converted_date.setText("0" + Integer.toString(dayOfYear) + yr);
}

I see what you are saying about the doc in Date. I read that too, but I'm concerned about the user putting in a wrong date liked 1/32/00 and NOT meaning 2/1/00. If it works this way, how do I know if it's calculating a leap year?
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Donna,
I am a little confused, because your code works as expected on my machine. Like we discussed, if the user put in a "wrong" date, it is still accepted. I don't think there is any way around that using the Calendar class. You would have to write your own data validation routine, like below:

Note that adding a key listener prevents the user from entering anything other than digits or slashes, so you really only need to check the validity of the month and date.
However, I don't understand why you say that
"I'm concerned about the user putting in a wrong date liked 1/32/00 and NOT meaning 2/1/00. If it works this way, how do I know if it's calculating a leap year?"
It seems to me that whether or not it is a leap year doesn't make any difference. For example, say the user enters "02/30/00". This is obviously wrong whether it is a leap year or not. But your code handles it as expected. 2000 IS a leap year, so February has 29 days. Therefore, "02/30/00" is interpreted as March 1, 2000, which is the 61st day of the year. If you take a year which is NOT a leap year, like 1999: "02/30/99" is interpreted as March 2, 1999, which is STILL the 61st day of the year.
Could you please explain your concern a little further?
Thanks.
Stephanie
Donna Meyer
Greenhorn

Joined: Sep 12, 2000
Posts: 21
Your input on this has been great! I guess I didn't explain my concern well enough though, but I think you example covers it. If the user puts in 09/40/00 instead of 09/04/00, the user would get unexpected results. He would really want output for the fourth day not the 10th day of the following month. See what I mean?
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Donna,
I have a suggestion for your project. It seems your main concern comes from the possibility of the user typing in a "wrong" value for the date. Why don't you eliminate this possibility using Choice instead of TextField. A brief example is below:

As you can see, when the user selects different months and years, the Choices update themselves accordingly.
What do you think?
Stephanie
[This message has been edited by Stephanie Grasson (edited September 13, 2000).]
[This message has been edited by Stephanie Grasson (edited September 13, 2000).]
Donna Meyer
Greenhorn

Joined: Sep 12, 2000
Posts: 21
I appreciate your suggestion and sample! I'm going to play around with it and see what I come up with. I can't think you enough for your imput. This is a really good forum ... certainly a good place to come when someone is at their height of frustration as I was!
Thanks again!
Donna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar Class