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.
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.
So the short answer to your question is: YES (deprecated).
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.
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.
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");
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With