将日期从默认语言环境转换为英语语言环境。

5

我通过使用SimpleDateFormat进行格式化,保存了日期。

DateFormat dateForm = new SimpleDateFormat("HH mm ss dd MMM ''yy"); 
            String dateOutput = dateForm.format(new Date());

这将使用设备的默认区域设置,例如法语或西班牙语。

如何将此格式化为不同区域设置的字符串转换回使用 Locale.ENGLISH 格式化的 Date 对象?

当前,当我尝试将字符串转换回日期时,会出现“无法解析的日期”异常。这是由于使用了不同的区域设置保存日期所致。


你需要将字符串转换为日期吗? - Viswanath Lekshmanan
@Arju 是的。我知道当字符串与英语相同的区域设置时如何做到这一点,但当它不是时就不知道了。 - user2442638
2个回答

11

这对你可能有用

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);

这对我来说是可行的,但不是最优雅的解决方案。请根据您的代码进行微调。


如何将格式不同的字符串转换为这种格式? - user2442638
你们支持哪些编程语言?这是针对所有语言的吗? - BigT
是的,但我目前遇到了在法语和英语之间切换的问题。 - user2442638
1
@RiThBo请查看修改。如果您能找到更好的解决方案,请告诉我。希望能找到更好的东西。 - BigT
谢谢!这对于法语<-->英语非常有效。有没有一种简单的方法可以使它在除了法语之外的所有语言中起作用?你能否只写 if(!loc.equals(Locale.ENGLISH.getDisplayLanguage())) - user2442638
我还没有找到其他语言之间的方法。这个链接很有趣。也许重新配置这个http://kodejava.org/how-do-i-format-date-using-a-locale-based-format/。 - BigT

-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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接