Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding day in the date

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

How can I know the day of the date e.g. 27/3/2011

Thanks & best regards
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Form a Calendar instance with the date value and get the day by getting Calendar.DAY_OF_WEEK
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Hope this helps

Thanks
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and if you are using Calendar.DAY_OF_WEEK note that Sunday = 1 & so on.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for fixing my problem

Best regards
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay Tidake wrote:System.out.println(cal.getTime().getDay());[/code]

Hope this helps

Not really. cal.getTime() returns a Date object, and Date.getDay() is deprecated and should not be used. You already have a Calendar object, use that. So once you have that, use cal.get(Calendar.DAY_OF_WEEK).

As for the return value, compare that to the constants Calendar.SUNDAY, Calendar.MONDAY, ..., Calendar.SATURDAY. These are valued from 1 to 7, but your code will be more readable and less error-prone.
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you simply wish to display the day of the week, it might be easier to use a %t tag and the printf method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic