Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date parse

 
anvi kon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
anvi kon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic