How can I get the names of days of week in JodaTime
Asked Answered
L

2

18

I've a problem to calculate an entire name list of days of week, using JodaTime. Pratically, I would like to see a similar output based on Locale:

1 day: Sunday 
2 day: Monday 
3 day: Tuesday 
4 day: Wednesday 
5 day: Thursday 
6 day: Friday 
7 day: Saturday

How can I do? I'm new in JodaTime libraries...

Thanks!

Laryssa answered 19/6, 2014 at 19:48 Comment(0)
J
23

From Joda-Time userguide:

For instance, the direct way to get the day of week for a particular DateTime, involves calling the method

int iDoW = dt.getDayOfWeek();

where iDoW can take the values (from class DateTimeConstants).

public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;
public static final int SUNDAY = 7;


...
 Localized versions of these methods are also available, thus
  DateTime.Property pDoW = dt.dayOfWeek();
  String strTF = pDoW.getAsText(Locale.FRENCH); // returns "Lundi", etc.

EDIT If using Default Locale

DateTime.Property pDoW = dt.dayOfWeek();
String strTF = pDoW.getAsText(Locale.getDefault());
Jubbah answered 19/6, 2014 at 20:3 Comment(1)
Thanks!! :) last question: how can I work with Locale.getDefault()? could be changed values from class DateTimeConstants?Laryssa
S
23

Looks like a job for DateTimeFormat

I would start with

 DateTime dt = new DateTime();
 DateTimeFormatter fmt = DateTimeFormat.forPattern("EEEE"); // use 'E' for short abbreviation (Mon, Tues, etc)
 String strEnglish = fmt.print(dt);
 String strFrench = fmt.withLocale(Locale.FRENCH).print(dt);
 String strWhereverUR = fmt.withLocale(Locale.getDefault()).print(dt);

and go from there

Seiber answered 19/6, 2014 at 20:1 Comment(2)
Thanks man! :) good solution...but, can I work with Locale.getdefault()? I wouldn't manage every Locale...Laryssa
Actually "E" pattern only prints "Fri". For "Friday" use "EEEE".Scrounge
J
23

From Joda-Time userguide:

For instance, the direct way to get the day of week for a particular DateTime, involves calling the method

int iDoW = dt.getDayOfWeek();

where iDoW can take the values (from class DateTimeConstants).

public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;
public static final int SUNDAY = 7;


...
 Localized versions of these methods are also available, thus
  DateTime.Property pDoW = dt.dayOfWeek();
  String strTF = pDoW.getAsText(Locale.FRENCH); // returns "Lundi", etc.

EDIT If using Default Locale

DateTime.Property pDoW = dt.dayOfWeek();
String strTF = pDoW.getAsText(Locale.getDefault());
Jubbah answered 19/6, 2014 at 20:3 Comment(1)
Thanks!! :) last question: how can I work with Locale.getDefault()? could be changed values from class DateTimeConstants?Laryssa

© 2022 - 2024 — McMap. All rights reserved.