Android: how to get the current day of the week (Monday, etc...) in the user's language?
Asked Answered
S

11

88

I want to know what the current day of the week is (Monday, Tuesday...) in the user's local language. For example, "Lundi" "Mardi" etc... if the user is French.

I have read this post, it but it only returns an int, not a string with the day in the user's language: What is the easiest way to get the current day of the week in Android?

More generally, how do you get all the days of the week and all the months of the year written in the user's language ?

I think that this is possible, as for example the Google agenda gives the days and months written in the user's local language.

Scratches answered 4/10, 2011 at 16:41 Comment(0)
M
186

Use SimpleDateFormat to format dates and times into a human-readable string, with respect to the users locale.

Small example to get the current day of the week (e.g. "Monday"):

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);
Mattson answered 4/10, 2011 at 16:48 Comment(0)
Y
38

Try this:

int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);  
String weekday = new DateFormatSymbols().getShortWeekdays()[dayOfWeek];
Yuu answered 27/10, 2014 at 23:58 Comment(2)
HI, if you using shortweekdays it showing only one letter.but i need to show first 3 letters of dayPsia
Note that to get access to all available locales one should use DateFormatSymbols.getInstance() (though for now it might give the same result).Aerograph
A
24

I know already answered but who looking for 'Fri' like this

for Fri -

SimpleDateFormat sdf = new SimpleDateFormat("EEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);

and who wants full date string they can use 4E for Friday

For Friday-

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);

Enjoy...

Anthelmintic answered 29/12, 2014 at 7:47 Comment(1)
I am getting an error on your 2nd line saying constructor Date is undefined.Tannenberg
X
18

To make things shorter You can use this:

android.text.format.DateFormat.format("EEEE", date);

which will return day of the week as a String.

Xanthous answered 6/5, 2012 at 22:29 Comment(0)
B
13

Hers's what I used to get the day names (0-6 means monday - sunday):

public static String getFullDayName(int day) {
    Calendar c = Calendar.getInstance();
    // date doesn't matter - it has to be a Monday
    // I new that first August 2011 is one ;-)
    c.set(2011, 7, 1, 0, 0, 0);
    c.add(Calendar.DAY_OF_MONTH, day);
    return String.format("%tA", c);
}

public static String getShortDayName(int day) {
    Calendar c = Calendar.getInstance();
    c.set(2011, 7, 1, 0, 0, 0);
    c.add(Calendar.DAY_OF_MONTH, day);
    return String.format("%ta", c);
}
Barquentine answered 4/10, 2011 at 16:54 Comment(0)
S
7

Try this...

//global declaration
private TextView timeUpdate;
Calendar calendar;

.......

timeUpdate = (TextView) findViewById(R.id.timeUpdate); //initialize in onCreate()

.......

//in onStart()
calendar = Calendar.getInstance();
//date format is:  "Date-Month-Year Hour:Minutes am/pm"
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm a"); //Date and time
String currentDate = sdf.format(calendar.getTime());

//Day of Name in full form like,"Saturday", or if you need the first three characters you have to put "EEE" in the date format and your result will be "Sat".
SimpleDateFormat sdf_ = new SimpleDateFormat("EEEE"); 
Date date = new Date();
String dayName = sdf_.format(date);
timeUpdate.setText("" + dayName + " " + currentDate + "");

The result is... enter image description here

Spiritism answered 9/8, 2014 at 5:39 Comment(0)
D
6

tl;dr

String output = 
    LocalDate.now( ZoneId.of( "America/Montreal" ) )
             .getDayOfWeek()
             .getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;

java.time

The java.time classes built into Java 8 and later and back-ported to Java 6 & 7 and to Android include the handy DayOfWeek enum.

The days are numbered according to the standard ISO 8601 definition, 1-7 for Monday-Sunday.

DayOfWeek dow = DayOfWeek.of( 1 );

This enum includes the getDisplayName method to generate a String of the localized translated name of the day.

The Locale object specifies a human language to be used in translation, and specifies cultural norms to decide issues such as capitalization and punctuation.

String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;

To get today’s date, use the LocalDate class. Note that a time zone is crucial as for any given moment the date varies around the globe.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
DayOfWeek dow = today.getDayOfWeek();
String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;

Keep in mind that the locale has nothing to do with the time zone.two separate distinct orthogonal issues. You might want a French presentation of a date-time zoned in India (Asia/Kolkata).

Joda-Time

UPDATE: The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

The Joda-Time library provides Locale-driven localization of date-time values.

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( zone );

Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter formatterUnJourQuébécois = DateTimeFormat.forPattern( "EEEE" ).withLocale( locale );

String output = formatterUnJourQuébécois.print( now );

System.out.println("output: " + output );

output: samedi


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Dagda answered 15/8, 2015 at 19:19 Comment(0)
A
4

Sorry for late reply.But this would work properly.

daytext=(textview)findviewById(R.id.day);

Calender c=Calender.getInstance();
SimpleDateFormat sd=new SimpleDateFormat("EEEE");
String dayofweek=sd.format(c.getTime());


daytext.setText(dayofweek);
Absurd answered 20/3, 2017 at 13:59 Comment(1)
(1) This solution is already covered in the accepted Answer. Explain how yours adds further value, or delete. (2) These classes are now legacy, supplanted by the java.time classes.Dagda
M
2

I just use this solution in Kotlin:

 var date : String = DateFormat.format("EEEE dd-MMM-yyyy HH:mm a" , Date()) as String
Mile answered 1/11, 2019 at 14:38 Comment(0)
L
2

If you are using ThreetenABP date library bt Jake Warthon you can do:

dayOfWeek.getDisplayName(TextStyle.FULL, Locale.getDefault()

on your dayOfWeek instance. More at: https://github.com/JakeWharton/ThreeTenABP https://www.threeten.org/threetenbp/apidocs/org/threeten/bp/format/TextStyle.html

Libel answered 12/12, 2019 at 13:41 Comment(0)
T
0
//selected date from calender
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy"); //Date and time
String currentDate = sdf.format(myCalendar.getTime());

//selcted_day name
SimpleDateFormat sdf_ = new SimpleDateFormat("EEEE");
String dayofweek=sdf_.format(myCalendar.getTime());
current_date.setText(currentDate);
lbl_current_date.setText(dayofweek);

Log.e("dayname", dayofweek);
Thermophone answered 16/7, 2019 at 6:3 Comment(1)
That’s unreadable. And you are using the poorly designed classes and long outdated SimpleDateFormat, Date and Calendar (also used in the question).Bourdon

© 2022 - 2024 — McMap. All rights reserved.