Mayank Pandya
Mayank Pandya

Reputation: 1623

Java convert long day name to short name

I got a day name as string like Monday, Tuesday or any week day name. but I want to display it as a short day format. like Mon, Tue and so on. Is there any proper way to do this?. Remember day name is a String. not a Date or Calendar.

Upvotes: 0

Views: 760

Answers (2)

morgano
morgano

Reputation: 17422

a Map<> or @Zorian's String.substring() are the best ways, another less efficient yet courious way:

SimpleDateFormat sdf = new SimpleDateFormat("EEE");
SimpleDateFormat sdf2 = new SimpleDateFormat("E");

System.out.println(">>> "  + sdf2.format(sdf.parse("Monday")));

Upvotes: 2

Zorian
Zorian

Reputation: 175

How about using String.substring(0,3);?
Through using substring, you get as result the first 3 chars out of your string:
Monday.substring(0,3); results in Mon

Upvotes: 3

Related Questions