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 Date parse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date parse" Watch "Date parse" New topic
Author

Date parse

anvi kon
Ranch Hand

Joined: Jan 08, 2010
Posts: 133
Hi,

I have a date like below, how would I parse it and get the daya month year.



but doj.getday() is not working.

Can anyone help me in this case?

Thanks,


Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

It works for me, assuming:

1. You enclose the date string in double quotes. Otherwise, it won't compile.
2. Take out the colon between the date and time. Otherwise, it won't parse.
3. Use doj.getDay() not doj.getday(). Again, it won't compile otherwise.

That returns 6, which means that the date is on a Saturday.

Also that's a deprecated call. The new Java specs say to use a Calendar rather than calling the functions on Date directly. I believe those methods have been deprecated for nearly 10 years already.
anvi kon
Ranch Hand

Joined: Jan 08, 2010
Posts: 133
thanks for your reply.

I have tried to use like bleow using Calendar.



It didn't work correctly.
I want the whatever the input string to be parsed(ie :2010-01-16 12:50:05)

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

anvi kon wrote:It didn't work correctly.

ItDoesntWorkIsUseless. What do you expect, and what do you get? You do know that the calendar months start at 0, not 1? So if mm is 0 then that means January. You should compare the return value against Calendar.JANUARY, Calendar.FEBRUARY, ..., Calendar.DECEMBER. The day of week is likewise just a number from Calendar.SUNDAY to Calendar.SATURDAY, inclusive.

To get a month starting at 1 just do a little calculation:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
anvi kon
Ranch Hand

Joined: Jan 08, 2010
Posts: 133
thanks,
But I except this way.



If I pass any DataString



//output
int yy = myCal.get(Calendar.YEAR); //except the value as 2010

int mm = myCal.get(Calendar.MONTH); //except the value as 01
for this one I am getting as 0

int ww = myCal.get(Calendar.DAY_OF_WEEK); //except the value as 16
for this one Iam getting as 5

hopefully, now you undesryand my problem.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Yes, I think I understand your problem... you don't know the difference between the day of the week (e.g Saturday) and the day of the month (e.g. the 16th).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

And I already told you why you got 0 (Calendar.JANUARY) for the month. I also showed you how to fix both issues.
anvi kon
Ranch Hand

Joined: Jan 08, 2010
Posts: 133
ok, thanks,


In the example, If I am passing the datatstring as "2010-06-16 12:50:05" then

int mm = myCal.get(Calendar.MONTH)

this should give me month as 6 , why is it giving as 0.

what should I do to get the month as 6 ? which should be a dynamic value.

Can anyone answer my question?

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

mm is minutes, not months. That's MM - capital M.

And again, as I said before, you will not get 6 but 5. Subtract Calendar.JANUARY, then add 1 to get 6.
anvi kon
Ranch Hand

Joined: Jan 08, 2010
Posts: 133
thanks,

But for june 06, I have done like below as you said, then still I am getting the value as 1.

int iMonth=ca1.get(Calendar.MONTH) - Calendar.JANUARY + 1;

Still result is 1

what I have to do to get june 06? Do I have to add 6 or 1?

Please let me know

thanks.


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Rob Prime wrote:mm is minutes, not months. That's MM - capital M.


Apparently you missed the significance of this (or just missed it entirely). It's referring to your parse string, not your Calendar-hacking code.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Check your input string. In almost all code you've given the date part is 2010-01-16 - January 16th. That would explain the 1 for the month.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date parse