如何将月份的语言更改为西班牙语?
How to change the language of the month to Spanish?
我收到 API 的日期是这样的:
2020-09-10T20:00:00.000Z
当我转换这个日期时,它显示 SEPTEMBER 10, 2020 8:00 p. m.
我需要用西班牙语显示月份,例如 Septiembre
或 Sep
您可以尝试这样的操作(returns 日期格式:10 de septiembre de 2020 20:00
):
val format: DateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, // date format
DateFormat.SHORT, // time format
Locale("es", "ES") // Spanish Locale
)
val dateTime = "2020-09-10T20:00:00.000Z"
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale("es", "ES"))
val date: Date = simpleDateFormat.parse(dateTime)!! // without validation
println(format.format(date)) // it prints `10 de septiembre de 2020 20:00`
我建议您使用 modern java.time
date-time API and the corresponding formatting API (package, java.time.format
) instead of with the outdated and error-prone java.util
date-time API and SimpleDateFormat
. Learn more about the modern date-time API from Trail: Date Time. If your Android API level is still not compliant with Java8, check and Java 8+ APIs available through desugaring。
使用现代 date-time API:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// The given date-time string
String strDateTime = "2020-09-10T20:00:00.000Z";
// Parse the given date-time string into OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
// Define the formatter for output in a custom pattern and in Spanish Locale
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd, uuuu hh:mm a", new Locale("es", "ES"));
// Print instant using the defined formatter
String formatted = formatter.format(odt);
System.out.println(formatted);
}
}
输出:
septiembre 10, 2020 08:00 p. m.
如果您仍想使用旧版 date-time 和格式 API,您可以按如下方式进行:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
// The given date-time string
String strDateTime = "2020-09-10T20:00:00.000Z";
// Define the formatter to parse the input string
SimpleDateFormat inputFormatter = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// Parse the given date-time string into java.util.Date
Date date = inputFormatter.parse(strDateTime);
// Define the formatter for output in a custom pattern and in Spanish Locale
SimpleDateFormat outputFormatter = new SimpleDateFormat("MMMM dd, yyyy hh:mm a", new Locale("es", "ES"));
// Print instant using the defined formatter
String formatted = outputFormatter.format(date);
System.out.println(formatted);
}
}
输出:
septiembre 10, 2020 08:00 p. m.
我收到 API 的日期是这样的:
2020-09-10T20:00:00.000Z
当我转换这个日期时,它显示 SEPTEMBER 10, 2020 8:00 p. m.
我需要用西班牙语显示月份,例如 Septiembre
或 Sep
您可以尝试这样的操作(returns 日期格式:10 de septiembre de 2020 20:00
):
val format: DateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, // date format
DateFormat.SHORT, // time format
Locale("es", "ES") // Spanish Locale
)
val dateTime = "2020-09-10T20:00:00.000Z"
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale("es", "ES"))
val date: Date = simpleDateFormat.parse(dateTime)!! // without validation
println(format.format(date)) // it prints `10 de septiembre de 2020 20:00`
我建议您使用 modern java.time
date-time API and the corresponding formatting API (package, java.time.format
) instead of with the outdated and error-prone java.util
date-time API and SimpleDateFormat
. Learn more about the modern date-time API from Trail: Date Time. If your Android API level is still not compliant with Java8, check
使用现代 date-time API:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// The given date-time string
String strDateTime = "2020-09-10T20:00:00.000Z";
// Parse the given date-time string into OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
// Define the formatter for output in a custom pattern and in Spanish Locale
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd, uuuu hh:mm a", new Locale("es", "ES"));
// Print instant using the defined formatter
String formatted = formatter.format(odt);
System.out.println(formatted);
}
}
输出:
septiembre 10, 2020 08:00 p. m.
如果您仍想使用旧版 date-time 和格式 API,您可以按如下方式进行:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
// The given date-time string
String strDateTime = "2020-09-10T20:00:00.000Z";
// Define the formatter to parse the input string
SimpleDateFormat inputFormatter = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// Parse the given date-time string into java.util.Date
Date date = inputFormatter.parse(strDateTime);
// Define the formatter for output in a custom pattern and in Spanish Locale
SimpleDateFormat outputFormatter = new SimpleDateFormat("MMMM dd, yyyy hh:mm a", new Locale("es", "ES"));
// Print instant using the defined formatter
String formatted = outputFormatter.format(date);
System.out.println(formatted);
}
}
输出:
septiembre 10, 2020 08:00 p. m.