• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert String Day of Week ("Monday") to Number (2) - via Inbuilt DateTime Classes

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I am getting string value from a web service in form of "MONDAY", "TUESDAY" upto "Sunday".

I am unable to get how to convert them into their corresponding integers using Calendar or other classes, so i did following




Is this method correct? Any other alternative which could prevent me to do this step?
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See java.text.DateFormatSymbols.getWeekdays()
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you i have used the above as shown below




Better

 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one problem in this, i need to send MONDAY as argument,

what i need was to no requirement of sending MONDAY to my own class for conversion....

some inbuilt which gives int value for monday by taking monday as it's parameter?
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Azrael Noor wrote:There is one problem in this, i need to send MONDAY as argument,

what i need was to no requirement of sending MONDAY to my own class for conversion....

some inbuilt which gives int value for monday by taking monday as it's parameter?



Why not just take the array index and add 1?
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use Strings as cases after switch.
You can use a Map<String, Integer>.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Azrael Noor wrote:There is one problem in this, i need to send MONDAY as argument,

what i need was to no requirement of sending MONDAY to my own class for conversion....

some inbuilt which gives int value for monday by taking monday as it's parameter?


I'm sorry I didn't get it. Could you please explain it a bit more?
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm sorry I didn't get it. Could you please explain it a bit more?



Sir i have "Monday" coming as String from somewhere.
I have to convert into it's equivalent integer
SUNDAY = 1
MONDAY = 2
TuesDAY = 3
WednesDAY = 4
ThrusDAY = 5
FriDAY = 6
SaturDAY = 7

I need to use IF and For's even using the method you suggest

I need some inbuilt class which just takes MONDAY as argument and return it second day of week i.e 2....

____________________

You can use Strings as cases after switch.


I am not able to use it with JDK 6

You can use a Map<String, Integer>.


looking into it thankyou

Why not just take the array index and add 1?


Sorry not able to get this!!!
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Declare a map, where the day name (lowercase) is the key!

K=Day
V=Integer/Index

WP
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about an enum?
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William are you saying like this?



Indeed nice method

Here i need to declare static Week Days Name.

I need Classes like Date, Calendar, XMLGregorianCalendar types which could deal with situation in few lines. If anyone have suggestion please share

till yet everyone give nice viewpoints, thank you all
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DON'T hard-code a String and then use it as a key. One typo can mess up everything. Note that each time you have tried to write "Thursday" you have misspelled the word as "Thrusday". That means that if you try to retrieve the value for that day with a String that is correctly spelled, you will get a big, fat null. Instead, get the Strings from the DateFormatSymbols just as you started to. Put those Strings in the map with the Integer value that is 1 more than their array index.

This bears repeating so I will repeat it. DON'T hard-code a String! You will be entering a world of pain.
 
William P O'Sullivan
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides the misspelling of "Thrusday", I agree with the fellow ranchers in that you should declare those strings as static constants or something else.

Also, keep everything in lower case, then use the compareToIgnoreCase() to find the value corresponding to the day.

Enums could also work for you, but then you may be dealing with valueOf() etc.. if you input doesn't match exactly.

WP
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean to say like this



Still i am seeking


I need Classes like Date, Calendar, XMLGregorianCalendar types which could deal with situation in few lines. If anyone have suggestion please share

till yet everyone give nice viewpoints, thank you all

 
Master Rancher
Posts: 4806
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure - use a SimpleDateFormat to parse the day of week (ignore all other fields; they won't matter). Create a GregorianCalendar and setTime() to the Date you just parsed, then use get(Calendar.DAY_OF_WEEK) to get a numeric value. Add or subtract as appropriate to get a different value.
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thats what i want

THANK YOU MIKE

TESTING IT MORE.........
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic