Convert date from default locale to english locale

2019-04-08 19:53发布

问题:

I have saved dates by formatting them using SimpleDateFormat.

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

This uses the default Locale of the device, for example French or Spanish.

How do I convert this string (formatted for a different Locale) back to a Date object formatted with Locale.ENGLISH?

Currently when I try to convert the string back into a date, I get an unparseable date exception. This is caused by the fact the the date was saved using a different Locale

回答1:

This can work for you

SimpleDateFormat sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.ENGLISH);
String dateOutput = sdf.format(new Date());

EDIT

The way I am thinking is to break it down with Calendar object and formatting it in another language. Try this

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

This does work for me however it is not the most elegant of solutions. Tweek it for your code.



回答2:

Try this class to convert Spanish Date To English or English Date to Spanish. You can change to cultures with a small change.

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