user1767444
user1767444

Reputation: 39

String.format to get the output date in dd-MMM-YYYY HH:mm pattern (Java)

String.format(start.toString("dd-MMM-YYYY HH:mm"));

where start is a date input in LocalDateTime class from org.joda.time api

when i am using this code, its returning month like this "Dec" but i want the output as "DEC".

Upvotes: 0

Views: 9015

Answers (4)

Sikumbang
Sikumbang

Reputation: 39

I always using substring, in my case like this :

String sDate = String.format(start.toString("dd-MMM-YYYY HH:mm"));
String oDate = sDate.substring(0, 2)+"-"+sDate.substring(3, 6).toUppercase()+"-"+sDate.substring(7, 11);

Upvotes: 1

Srinivas B
Srinivas B

Reputation: 1852

If this (String.format(start.toString("dd-MMM-YYYY HH:mm"));) retrieves the correct format of what you want, then you can simply use

String.format(start.toString("dd-MMM-YYYY HH:mm")).toUpperCase();

Upvotes: 2

Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

The only way will be to replace a substring by the uppercase version.

Date start = new Date();
SimpleDateFormat oFormat = new SimpleDateFormat("dd-MMM-YYYY HH:mm");
String sDate = oFormat.format(start);

System.out.println(sDate);

sDate = sDate.substring(0,3) + sDate.substring(3,6).toUpperCase() +  sDate.substring(6,sDate.length());

System.out.println(sDate);

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

If you want a specific case, I would use .toUpperCase()

Upvotes: 1

Related Questions