2016-07-22 5 views
0

「Udacity Sunshine App」の「Utility.Java」ファイルから取得した、以下のコードスニペット。 "Date"データはサーバーから取り出し、この方法で表示します。ロケールをサポートするために、以下のコードを変更する方法

ロケールをサポートするために、アンドロイドスタジオのコードを変更したいと思います。そして最後に、異なる言語で日と月の名前を返します。お願い助けて?

static String formatDate(long dateInMilliseconds) { 
    Date date = new Date(dateInMilliseconds); 
    return DateFormat.getDateInstance().format(date); 
} 

// Format used for storing dates in the database. ALso used for converting those strings 
// back into date objects for comparison/processing. 
public static final String DATE_FORMAT = "yyyyMMdd"; 

/** 
* Helper method to convert the database representation of the date into something to display 
* to users. As classy and polished a user experience as "20140102" is, we can do better. 
* 
* @param context Context to use for resource localization 
* @param dateInMillis The date in milliseconds 
* @return a user-friendly representation of the date. 
*/ 
public static String getFriendlyDayString(Context context, long dateInMillis, boolean displayLongToday) { 
    // The day string for forecast uses the following logic: 
    // For today: "Today, June 8" 
    // For tomorrow: "Tomorrow" 
    // For the next 5 days: "Wednesday" (just the day name) 
    // For all days after that: "Mon Jun 8" 

    Time time = new Time(); 
    time.setToNow(); 
    long currentTime = System.currentTimeMillis(); 
    int julianDay = Time.getJulianDay(dateInMillis, time.gmtoff); 
    int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff); 

    // If the date we're building the String for is today's date, the format 
    // is "Today, June 24" 
    if (displayLongToday && julianDay == currentJulianDay) { 
     String today = context.getString(R.string.today); 
     int formatId = R.string.format_full_friendly_date; 
     return String.format(context.getString(
       formatId, 
       today, 
       getFormattedMonthDay(context, dateInMillis))); 
    } else if (julianDay < currentJulianDay + 7) { 
     // If the input date is less than a week in the future, just return the day name. 
     return getDayName(context, dateInMillis); 
    } else { 
     // Otherwise, use the form "Mon Jun 3" 
     SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd"); 
     return shortenedDateFormat.format(dateInMillis); 
    } 
} 

/** 
* Helper method to convert the database representation of the date into something to display 
* to users. As classy and polished a user experience as "20140102" is, we can do better. 
* 
* @param context Context to use for resource localization 
* @param dateInMillis The date in milliseconds 
* @return a user-friendly representation of the date. 
*/ 
public static String getFullFriendlyDayString(Context context, long dateInMillis) { 

    String day = getDayName(context, dateInMillis); 
    int formatId = R.string.format_full_friendly_date; 
    return String.format(context.getString(
      formatId, 
      day, 
      getFormattedMonthDay(context, dateInMillis))); 
} 

/** 
* Given a day, returns just the name to use for that day. 
* E.g "today", "tomorrow", "wednesday". 
* 
* @param context Context to use for resource localization 
* @param dateInMillis The date in milliseconds 
* @return 
*/ 
public static String getDayName(Context context, long dateInMillis) { 
    // If the date is today, return the localized version of "Today" instead of the actual 
    // day name. 

    Time t = new Time(); 
    t.setToNow(); 
    int julianDay = Time.getJulianDay(dateInMillis, t.gmtoff); 
    int currentJulianDay = Time.getJulianDay(System.currentTimeMillis(), t.gmtoff); 
    if (julianDay == currentJulianDay) { 
     return context.getString(R.string.today); 
    } else if (julianDay == currentJulianDay +1) { 
     return context.getString(R.string.tomorrow); 
    } else { 
     Time time = new Time(); 
     time.setToNow(); 
     // Otherwise, the format is just the day of the week (e.g "Wednesday". 
     SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE"); 
     return dayFormat.format(dateInMillis); 
    } 
} 

/** 
* Converts db date format to the format "Month day", e.g "June 24". 
* @param context Context to use for resource localization 
* @param dateInMillis The db formatted date string, expected to be of the form specified 
*    in Utility.DATE_FORMAT 
* @return The day in the form of a string formatted "December 6" 
*/ 
public static String getFormattedMonthDay(Context context, long dateInMillis) { 
    Time time = new Time(); 
    time.setToNow(); 
    SimpleDateFormat dbDateFormat = new SimpleDateFormat(Utility.DATE_FORMAT); 
    SimpleDateFormat monthDayFormat = new SimpleDateFormat("MMMM dd"); 
    String monthDayString = monthDayFormat.format(dateInMillis); 
    return monthDayString; 
} 
+0

にロケールを追加します。 SimpleDateFormat dayFormat =新しいSimpleDateFormat( "EEEE"、Locale.US); String dayName = dayFormat.format(dateInMillis); if(dayName == "Monday"){ return context.getString(R.string.day_name_1); } else if(dayName == "Tuesday"){ return context.getString(R.string.day_name_2); } //他の場合は... // else if(dayName == "Sunday"){ return context.getString(R.string.day_name_7); } else return null; –

+0

あなたの質問にそれを編集し、コード – Ares

答えて

1

ちょうど私がその日の名前のために、これを試しにSimpleDateFormatのコンストラクタ

SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE MMM dd", Locale.ENGLISH); 
return dateFormat.format(dateInMillis); 
+0

としてフォーマットしてください。これは、曜日と月の名前を英語で返します。 –

+0

Locale.Frenchのように、ロケールを任意のものに変更してください。
完全な日付を使用する場合は、 SimpleDateFormat( "EEE MMM dd yyyy"、Locale.French); – lionscribe

+0

私はAmharic言語(エチオピア語)をデフォルトとして使用します。 –

関連する問題