我希望我的字符串转换为一天
I want my string to converted to a day
我有这个字符串
String s = "29/04/2015"
我希望它用我的语言(挪威语)生成那天的名称。
例如:
29/04/2015
是 "Onsdag"
30/04/2015
是 "Torsdag"
我该怎么做?
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);
现在 day
将在给定的语言环境中度过这一天。更新
首先你需要一个 Calendar
对象。
Calendar cal = Calendar.getInstance();
String s = "29/04/2015"
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
cal.setTime(format.parse(s));
从 Calendar
你可以得到星期几。
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
星期几为 1-7,星期日(英文)为 1
您需要使用您的语言环境配置一个 DateFormat 实例(查看 https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html)。
然后按照 Dilip 的建议解析日期并获取日期。
首先,您需要将 String
解析为 Date
。然后使用 Calendar
获取星期几。您可以使用数组将其转换为适当的字符串。
// 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]);
您可以使用 HashMap 映射,其中第一个参数是日期“29/4/2015”,第二个是含义。您可以使用您的字符串来获取含义 map.get (yourString).
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;
....
}
编辑:我刚刚对此进行了测试,它实际上是从周日开始的,returns 周日的值 1,我已经更改了常量值以反映这一点。
您可以结合使用日期解析和区域设置来获得所需的输出。例如参考以下代码。
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);
有关语言环境的详细信息,请访问 Oracle Java Documentation。
您需要解析您的带有日期的文本到Date
实例,然后格式化返回文本。您可以使用 SimpleDateFormat
class 来实现,它支持多种日期模式,例如
dd/MM/yyyy
为您的原始日期,
- 和
EEEE
用于月份中日期的全称。
格式化时您还需要指定要使用的语言环境。要创建特定于挪威的语言环境,您可以使用
Locale nor = Locale.forLanguageTag("no-NO");
因此您的代码看起来或多或少类似于:
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));
输出:onsdag
.
我有这个字符串
String s = "29/04/2015"
我希望它用我的语言(挪威语)生成那天的名称。
例如:
29/04/2015
是 "Onsdag"30/04/2015
是 "Torsdag"
我该怎么做?
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);
现在 day
将在给定的语言环境中度过这一天。更新
首先你需要一个 Calendar
对象。
Calendar cal = Calendar.getInstance();
String s = "29/04/2015"
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
cal.setTime(format.parse(s));
从 Calendar
你可以得到星期几。
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
星期几为 1-7,星期日(英文)为 1
您需要使用您的语言环境配置一个 DateFormat 实例(查看 https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html)。
然后按照 Dilip 的建议解析日期并获取日期。
首先,您需要将 String
解析为 Date
。然后使用 Calendar
获取星期几。您可以使用数组将其转换为适当的字符串。
// 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]);
您可以使用 HashMap 映射,其中第一个参数是日期“29/4/2015”,第二个是含义。您可以使用您的字符串来获取含义 map.get (yourString).
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;
....
}
编辑:我刚刚对此进行了测试,它实际上是从周日开始的,returns 周日的值 1,我已经更改了常量值以反映这一点。
您可以结合使用日期解析和区域设置来获得所需的输出。例如参考以下代码。
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);
有关语言环境的详细信息,请访问 Oracle Java Documentation。
您需要解析您的带有日期的文本到Date
实例,然后格式化返回文本。您可以使用 SimpleDateFormat
class 来实现,它支持多种日期模式,例如
dd/MM/yyyy
为您的原始日期,- 和
EEEE
用于月份中日期的全称。
格式化时您还需要指定要使用的语言环境。要创建特定于挪威的语言环境,您可以使用
Locale nor = Locale.forLanguageTag("no-NO");
因此您的代码看起来或多或少类似于:
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));
输出:onsdag
.