如何创建一个获取当前日期并返回月份名称的函数?
我只有日期而不是当前日期,它可以是任何日期,如 2013/4/12 或 23/8/8。
就像String monthName("2013/9/11");
调用此函数时返回月份名称一样。
这应该没问题。
这取决于日期的格式。如果您尝试使用 2011 年 2 月 1 日,它会起作用,只需根据您的需要更改此字符串“MMMM d, yyyy”。检查所有格式模式。
而且,月份是从 0 开始的,所以如果你希望一月是 1,只需返回月份 + 1
private static int getMonth(String date) throws ParseException{
Date d = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int month = cal.get(Calendar.MONTH);
return month + 1;
}
如果你想要月份名称试试这个
private static String getMonth(String date) throws ParseException{
Date d = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String monthName = new SimpleDateFormat("MMMM").format(cal.getTime());
return monthName;
}
正如我所说,检查我发布的所有格式模式的网页。如果您只想要月份的 3 个字符,请使用“MMM”而不是“MMMM”
我正在贡献现代答案。
System.out.println(LocalDate.of(2013, Month.SEPTEMBER, 11) // Define the date
.getMonth() // Get the month
.getDisplayName( // Get the month name
TextStyle.FULL_STANDALONE, // No abbreviation
Locale.ENGLISH)); // In which language?
输出是:
九月
LocalDate
java.time(现代 Java 日期和时间 API)作为日期。LocalDate.getMonth()
andMonth.getDisplayName()
获取月份名称。Date
,Calendar
并SimpleDateFormat
在 2013 年的旧答案中使用。这些课程设计不良、麻烦且过时。现代 API 使用起来要好得多。也避免switch
/case
为此目的,因为月份名称已经内置,并且使用库方法可以让您更清晰、更简洁且不易出错的代码。 LocalDate today = LocalDate.now(ZoneId.systemDefault());
LocalDate aDate = LocalDate.of(2013, Month.SEPTEMBER, 11); // 2013/9/11
LocalDate anotherDate = LocalDate.of(2023, 8, 8); // 23/8/8
如果您将日期作为字符串输入,请使用 a 解析字符串DateTimeFormatter
:
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("u/M/d");
String stringInput = "2013/4/12";
LocalDate date = LocalDate.parse(stringInput, dateFormatter);
System.out.println(date);
2013-04-12
要获得月份名称,您首先需要确定您想要月份名称的语言。我以英语为例,仍然使用date
上一个片段:
String monthName = date.getMonth()
.getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH);
System.out.println(monthName);
四月
Java 知道多种语言的月份名称。如果您想要用户语言的月份名称,请将其Locale.getDefault()
作为第二个参数传递给getDisplayName()
.
Oracle 教程:日期时间解释如何使用 java.time。
使用此代码 -
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
因此,现在您有了月份编号,您可以使用 switch case 来获取该月份的名称。
如果您的日期是字符串格式,请使用 -
Date date = new SimpleDateFormat("yyyy-MM-dd").format(d)
我正在使用这样的功能:
public String getDate(String startDate) throws ParseException {
@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = sdf.parse(startDate);
sdf.applyPattern("MMMM dd, YYYY"); //this gives output as=> "Month date, year"
} catch (Exception e) {
Log.d("Exception", e.getLocalizedMessage());
}
return sdf.format(d);
}
按名称获取当前月份的简单解决方案:
SimpleDateFormat formatterMonth = new SimpleDateFormat("MMMM");
String currentMonth = formatterMonth.format(new Date(System.currentTimeMillis()));
使用格式 2013/9/11 按名称获取任何月份的功能:(未测试)
private String monthName(String dateToCheck){
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
date = formatter.parse(dateToCheck);
SimpleDateFormat formatterMonth = new SimpleDateFormat("MMMM");
return formatterMonth.format(new Date(date.getTime()));
}
您可以获得另一个答案中描述的月份的“数字”,然后您可以简单地使用开关来获取名称。例子:
switch(month) {
case 0:
your name is January
break;
...
}
PS 我认为月份是从零开始的,但我不是 100% 确定...