Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get short month names in Joda Time?

Tags:

Does anyone know if there's a method in Joda Time or Java itself which takes either an int or a String as an argument, e.g. 4 or "4" and gives the name of the month back in short format, i.e. JAN for January?

I suppose long month names can be truncated and converted to upper case.

like image 419
Mr Morgan Avatar asked Jun 11 '10 19:06

Mr Morgan


People also ask

How do I change the date format in Joda-time?

How to change the SimpleDateFormat to jodatime? String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat. forPattern("yyyy-MM-dd'T'HH:mm:ss. SSSS"); DateTime instant = dtf.

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

What is Joda-time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.

Does Joda DateTime have time zones?

Adjusting Time ZoneUse the DateTimeZone class in Joda-Time to adjust to a desired time zone. Joda-Time uses immutable objects. So rather than change the time zone ("mutate"), we instantiate a new DateTime object based on the old but with the desired difference (some other time zone). Use proper time zone names.


2 Answers

In response to Jon's answer, you can further simplify that by using Joda's direct access for datetime classes.

String month = date.toString("MMM"); 
like image 89
puug Avatar answered Sep 18 '22 15:09

puug


I believe "MMM" will give the month name in Joda... but you'd need to build up an appropriate formatter first. Here's some sample code which prints "Apr" on my box. (You can specify the relevant locale of course.)

import org.joda.time.*; import org.joda.time.format.*;  public class Test {     public static void main(String[] args)     {         // Year and day will be ignored         LocalDate date = new LocalDate(2010, 4, 1);         DateTimeFormatter formatter = DateTimeFormat.forPattern("MMM");         String month = formatter.print(date);         System.out.println(month);     } } 
like image 26
Jon Skeet Avatar answered Sep 20 '22 15:09

Jon Skeet