-2

I have this string

String s = "29/04/2015"

And I want it to produce the name of that day in my language, which is Norwegian.

For example:

  • 29/04/2015 is "Onsdag"
  • 30/04/2015 is "Torsdag"

How can I do this?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user2891133
  • 309
  • 2
  • 4
  • 13
  • Sorry, i have no sample code. – user2891133 May 01 '15 at 15:28
  • You need a locale specific DateFormat. This is an easy problem. – duffymo May 01 '15 at 15:31
  • Do you have a code example? – user2891133 May 01 '15 at 15:32
  • 1
    If you don't have a sample code at least tell us what you have tried so far. Don't expect that someone here will just write all the code for you. We are eager to help but first show us that you have actually tried something before posting. – Rafal G. May 01 '15 at 15:32
  • Try locale specific date format - https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat-java.lang.String-java.text.DateFormatSymbols- – saugata May 01 '15 at 15:37

8 Answers8

2
String dateString = "29/04/2015";
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse(dateString);
SimpleDateFormat formatter = new SimpleDateFormat("E", Locale.no_NO);
String day = formatter.format(date);

Now day will have the day in given locale. Update

Dilip Kumar
  • 2,504
  • 2
  • 17
  • 33
0

First you'll need a Calendar object.

Calendar cal = Calendar.getInstance();    
String s = "29/04/2015"
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
cal.setTime(format.parse(s));

From the Calendar you can get the day of the week.

int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

dayOfWeek will be 1-7 with Sunday (in english) being 1

barbiepylon
  • 891
  • 7
  • 23
0

You need to configure an instance of DateFormat, with your locale, (take a look at https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html).

then parse the Date and get the day, as Dilip already suggests.

duffy356
  • 3,678
  • 3
  • 32
  • 47
0

First you will need to parse the String to a Date. Then use a Calendar to get the day of the week. You can use an array to convert it to the appropriate string.

// Array of Days
final String[] DAYS = {
   "søndag", "mandag", "tirsdag", "onsdag", "torsdag", "fredag", "lørdag"
};

// Parse the date
final String source = "27/04/2015";
final DateFormat format = new SimpleDateFormat("dd/MM/yyyy");

Date date = new Date();
try {
   date = format.parse(source);
} catch (final ParseException e) {
   e.printStackTrace();
}

// Convert to calendar
final Calendar c = Calendar.getInstance();
c.setTime(date);
final int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

// Get the day's name
System.out.println("Day of Week: " + dayOfWeek);
System.out.println("Day = " + DAYS[dayOfWeek - 1]);
Dan Grahn
  • 9,044
  • 4
  • 37
  • 74
0
final int SUNDAY = 1;
final int ONSDAG = 2;
final int TORSDAG = 3;
....
....

String s = "29/04/2015";
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse(s);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DAY_OF_WEEK);

String dayString;
switch (day) {
     case(ONSDAG):
         dayString = "ONSDAG";
         break;
        ....
}

EDIT: I just tested this and it actually starts from Sunday, and returns the value of 1 for sunday, I've changed the constant values to reflect this.

dahui
  • 2,128
  • 2
  • 21
  • 40
0

You can use date parsing combined with Locale settings to get the desired output. For e.g. refer following code.

String dateStr = "29/04/2015";

SimpleDateFormat dtf = new SimpleDateFormat("dd/MM/yyyy");
Date dt = dtf.parse(dateStr);

Calendar cal = Calendar.getInstance();
cal.setTime(dt);
String m = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG_FORMAT, new Locale("no", "NO"));

System.out.println(m);

For more information about locale, visit Oracle Java Documentation.

CuriousMind
  • 3,143
  • 3
  • 29
  • 54
0

You need to parse your text with date to Date instance and then format it back to text. You can do it with SimpleDateFormat class which supports many patterns of dates like

  • dd/MM/yyyy for your original date,
  • and EEEE for full name of day in month.

While formatting you will also need to specify locale you want to use. To create Norway specific locale you can use for instance

Locale nor = Locale.forLanguageTag("no-NO");

So your code can look more or less like:

String text = "29/04/2015";

Locale nor = Locale.forLanguageTag("no-NO");

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", nor);
SimpleDateFormat dayOfWeek = new SimpleDateFormat("EEEE", nor);

Date date = sdf.parse(text);
System.out.println(dayOfWeek.format(date));

Output: onsdag.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
-4

You can use an HashMap map where the first parametri is the date "29/4/2015" while the second is the meaning. You can use your string to get the meaning map.get (yourString).

Skizzo
  • 2,883
  • 8
  • 52
  • 99