我已使用SimpleDateFormat
格式化日期来保存日期。
DateFormat dateForm = new SimpleDateFormat("HH mm ss dd MMM ''yy");
String dateOutput = dateForm.format(new Date());
这使用设备的默认语言环境,例如法语或西班牙语。
如何将此字符串(针对其他区域设置格式化)转换回Date
格式的Locale.ENGLISH
对象?
目前,当我尝试将字符串转换回日期时,我得到一个不可解析的日期异常。这是因为使用不同的区域设置保存日期
答案 0 :(得分:9)
这对你有用
SimpleDateFormat sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.ENGLISH);
String dateOutput = sdf.format(new Date());
修改强>
我的想法是用Calendar
对象分解并用另一种语言格式化它。试试这个
SimpleDateFormat sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
Calendar cal = Calendar.getInstance();
int day = 0, month = 0, year = 0, hour = 0, minute = 0, sec = 0;
String loc = Locale.getDefault().getDisplayLanguage();
try {
Date testDate = sdf.parse(date);
cal.setTime(testDate);
sec = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
} catch (ParseException e) {
if(loc.equals(Locale.ENGLISH.getDisplayLanguage())){
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.FRENCH);
try {
Date testDate = sdf.parse(date);
cal.setTime(testDate);
sec = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
} catch (ParseException ee) {
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.FRENCH);
try {
Date testDate = sdf.parse(date);
cal.setTime(testDate);
sec = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
} catch (ParseException eex) {
}
}
}
else if (loc.equals(Locale.FRENCH.getDisplayLanguage())){
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.ENGLISH);
try {
Date testDate = sdf.parse(date);
cal.setTime(testDate);
sec = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
} catch (ParseException fe) {
}
}
}
cal.set(Calendar.SECOND, sec);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
if(loc.equals(Locale.ENGLISH.getDisplayLanguage()))
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
else if(loc.equals(Locale.FRENCH.getDisplayLanguage()))
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
Date convertedLangDate = cal.getTime();
String newDate = sdf.format(convertedLangDate);
这对我有用,但它不是最优雅的解决方案。为你的代码调整它。
答案 1 :(得分:-1)
尝试使用此课程将西班牙语日期转换为英语或英语日期为西班牙语。你可以通过一些小改变来改变文化。
Imports System
Imports System.Globalization
Imports System.Threading
Imports Microsoft.VisualBasic
Public Class ClassDateLanguageConversions
Public Function SpanishToEnglish( _
p_strSpanishDateTime As String, _
Optional p_blnFormal As Boolean = False _
) As DateTime
SpanishToEnglish = Nothing
Try
Dim strReformattedDate As String = p_strSpanishDateTime
'Handle the abiguity of Tuesday abbrieviation
' and March abrieviation by normalizing
strReformattedDate = strReformattedDate.ToLower.Replace( _
"martes, marzo", "mar, marzo")
strReformattedDate = strReformattedDate.ToLower.Replace( _
"martes marzo", "mar, marzo")
strReformattedDate = strReformattedDate.ToLower.Replace( _
"mar marzo", "mar, marzo")
strReformattedDate = strReformattedDate.ToLower.Replace( _
"martes, mar", "mar, marzo")
strReformattedDate = strReformattedDate.ToLower.Replace( _
"martes mar", "mar, marzo")
strReformattedDate = strReformattedDate.ToLower.Replace( _
"mar, mar", "mar, marzo")
strReformattedDate = strReformattedDate.ToLower.Replace( _
"mar mar", "mar, marzo")
'Shorten final date string by default
If Not p_blnFormal Then
strReformattedDate = strReformattedDate.ToLower.Replace( _
" de ", " ") 'Remove "the"
strReformattedDate = strReformattedDate.ToLower.Replace( _
" el ", " ") 'Remove "on the"
End If
Thread.CurrentThread.CurrentCulture = New CultureInfo("es")
Dim datetimeSpanish As DateTime = DateTime.Parse(strReformattedDate)
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
SpanishToEnglish = datetimeSpanish
Catch ex As Exception
'Returns null
End Try
End Function
Public Function EnglishToSpanish( _
ByVal p_datetimeEnglishDateTime As DateTime, _
Optional ByVal p_blnFormal As Boolean = False _
) As String
EnglishToSpanish = ""
Try
Thread.CurrentThread.CurrentCulture = New CultureInfo("es")
Dim strSpanishDate As String = _
p_datetimeEnglishDateTime.ToLongDateString
'Shorten final date string by default
If Not p_blnFormal Then
strSpanishDate = strSpanishDate.Replace( _
" de ", " ") 'Remove "the"
strSpanishDate = strSpanishDate.Replace( _
" el ", " ") 'Remove "on the"
End If
EnglishToSpanish = strSpanishDate
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
Catch ex As Exception
'Returns null
End Try
End Function
End Class
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim objClassDateLanguageConversions As New ClassDateLanguageConversions
Dim datetimeEnglish1 As DateTime = objClassDateLanguageConversions.SpanishToEnglish("lunes, 24 de marzo de 2014")
Dim datetimeEnglish2 As DateTime = objClassDateLanguageConversions.SpanishToEnglish("Mar, mar 4 2014")
Dim datetimeEnglish3 As DateTime = objClassDateLanguageConversions.SpanishToEnglish("Lun, marzo 24 2014")
Dim datetimeEnglish4 As DateTime = objClassDateLanguageConversions.SpanishToEnglish("lun, abr 28 2014")
Dim datetimeEnglish5 As DateTime = objClassDateLanguageConversions.SpanishToEnglish("lunes, 24 marzo 2014")
Dim strSpanish1 As String = objClassDateLanguageConversions.EnglishToSpanish(Now)
Dim datetimeEnglish10 As DateTime = objClassDateLanguageConversions.SpanishToEnglish(strSpanish1)
Dim strSpanish2 As String = objClassDateLanguageConversions.EnglishToSpanish("Mar 4, 2014")
Dim datetimeEnglish11 As DateTime = objClassDateLanguageConversions.SpanishToEnglish(strSpanish2)
Dim strSpanish3 As String = objClassDateLanguageConversions.EnglishToSpanish("Mar 4, 2014 9:00pm")
Dim datetimeEnglish12 As DateTime = objClassDateLanguageConversions.SpanishToEnglish(strSpanish3)
Dim strSpanish4 As String = objClassDateLanguageConversions.EnglishToSpanish("Thursday May 1, 2014")
Dim datetimeEnglish13 As DateTime = objClassDateLanguageConversions.SpanishToEnglish(strSpanish4)
End Sub
End Class