Easiest/shortest way to get day of week (e.g. MON) from "dd.mm.yyyy" string?

S

Sven Fischer

Assume I have a date as a string with the pattern "dd.mm.yyyy".

What is the easiest/shortest way to get the day of week (e.g. "MON" for Monday)
from this date?

Sven
 
J

jlp

Le 21/11/2011 17:58, Sven Fischer a écrit :
Assume I have a date as a string with the pattern "dd.mm.yyyy".

What is the easiest/shortest way to get the day of week (e.g. "MON" for Monday)
from this date?

Sven
create a Calendar object with the date and Calendar.get( DAY_OF_WEEK)
return 0 for SUNDAY, 1 for MONDAY ...
 
A

Andreas Leitgeb

Sven Fischer said:
Assume I have a date as a string with the pattern "dd.mm.yyyy".
What is the easiest/shortest way to get the day of week (e.g. "MON" for Monday)
from this date?

Look at java.text.SimpleDateFormat (for parsing the string)
and at java.util.Calendar (for extracting the day of week)
 
J

jlp

Le 21/11/2011 18:40, jlp a écrit :
Le 21/11/2011 17:58, Sven Fischer a écrit :
create a Calendar object with the date and Calendar.get( DAY_OF_WEEK)
return 0 for SUNDAY, 1 for MONDAY ...
Sorry SUNDAY is 1, MONDAY 2 ....
 
M

Mayeul

Assume I have a date as a string with the pattern "dd.mm.yyyy".

What is the easiest/shortest way to get the day of week (e.g. "MON" for Monday)
from this date?

Sven

If you ask me the /simplest/ way to do that is with two SimpleDateFormats:
- one to parse the pattern to a Date,
- the other just to format the Date to corresponding day of the week.

Then some toUpperCase(Locale.US) to obtain MON instead of Mon.
Forcing the second SimpleDateFormat to Locale.US might be preferable.

Sounds lazy, probably quite inefficient, and is not flexible at all.
But it is a three-liners, with very short lines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top