簡體   English   中英

日期格式:Android聯系人生日周年紀念日

[英]Date format : Android Contacts Birthday Anniversary

致力於聯系生日和周年紀念日:

我得到的詳細信息和生日就像這個12.2.2012或12/2/2012或12-02-2012或2/12/12

題:

  1. 所有三星手機的日期格式是否相同。 如果是,日期格式是什么。

    (猜猜不適用於所有Android手機,因為生日日期以多種不同的格式存儲)

  2. 如何識別日期格式,如日期是2012年2月12日或2012年2月12日或任何其他日期字符串模式。 是格式“yyyy-MM-dd”還是“MMM dd,yyyy”或其他? 例如:如果日期是“2012年2月12日”,則日期格式為“MMM dd yyyy”

不幸的是,似乎不同的應用/廠商使用不同的格式 我的解決方案是嘗試解析不同的格式,直到我找到正確的或放棄。

以下是一些示例代碼:

public static final SimpleDateFormat[] birthdayFormats = {
    new SimpleDateFormat("yyyy-MM-dd"),
    new SimpleDateFormat("yyyyMMdd"),
    new SimpleDateFormat("yyyy.MM.dd"),
    new SimpleDateFormat("yy-MM-dd"),,
    new SimpleDateFormat("yyMMdd"),
    new SimpleDateFormat("yy.MM.dd")
    new SimpleDateFormat("yy/MM/dd"),
    new SimpleDateFormat("MM-dd"),
    new SimpleDateFormat("MMdd"),
    new SimpleDateFormat("MM/dd"),
    new SimpleDateFormat("MM.dd"),
};
.....
    Date birthday = null;
        for (SimpleDateFormat f : birthdayFormats) {
        try {
                birthday = f.parse(birthdaystr);
                if (birthday!=null) {
                        contact.setBirthday(birthday);
                        break;
                 }
        } catch (Exception e) {
                continue;
        }
            }

使用DateFormat.getDateInstance(int style, Locale locale)而不是使用SimpleDateFormat創建自己的模式。

你希望在String中獲取日期並在傳遞下面的代碼之后的另一種方式

String dateStr = "04/05/2010"; 

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
Date dateObj = curFormater.parse(dateStr); 
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy"); 

String newDateStr = postFormater.format(dateObj); 

日期存儲為“YYYY-MM-DD”。

使用日期格式化程序類將其轉換為您需要的任何格式。

當您想要更新時,請將其放入您閱讀的格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM