簡體   English   中英

在android中將字符串轉換為日期格式

[英]Convert string to date format in android

我正在嘗試將字符串轉換為日期格式。我嘗試了很多方法來做到這一點。但沒有成功。 我的字符串是“2012 年 1 月 17 日”。 我想將其轉換為“2011-10-17”。 有人可以告訴我這樣做的方法嗎? 如果您有任何已完成的示例,那將是真正的幫助!

try {

     String strDate = "Jan 17, 2012";

     //current date format
     SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");

     Date objDate = dateFormat.parse(strDate);

     //Expected date format
     SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");

     String finalDate = dateFormat2.format(objDate);

     Log.d("Date Format:", "Final Date:"+finalDate)

   } catch (Exception e) {
      e.printStackTrace();
 }
   String format = "yyyy-MM-dd";
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));

在 PDT 時區運行時會產生此輸出:

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01

欲了解更多信息,請看這里

我建議使用Joda Time ,它是 Java 中最好和最簡單的日期/日期時間操作庫,它是 ThreadSafe(與 Java 中的默認格式類相反)。

你這樣使用它:

// Define formatters:
DateTimeFormatter inputFormat = DateTimeFormat.forPattern("MMM dd, yyyy");
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy-MM-dd");

// Do your conversion:
String inputDate = "Jan 17, 2012";
DateTime date = inputFormat.parseDateTime(inputDate);
String outputDate = outputFormat.print(date);
// or:
String outputDate = date.toString(outputFormat);
// or:
String outputDate = date.toString("yyyy-MM-dd");

// Result: 2012-01-17

它還提供了大量有用的日期操作方法(添加日期、時差等)。 並且它提供了大多數類的接口,以便於測試和依賴注入。

public static Date getDateFromString(String date) {

    Date dt = null;

    if (date != null) {
        for (String sdf : supportedDateFormats) {
            try {
                dt = new Date(new SimpleDateFormat(sdf).parse(date).getTime());
                break;
            } catch (ParseException pe) {
                pe.printStackTrace();
            }
        }
    }
    return dt;
}

為什么要將字符串轉換為字符串嘗試將當前時間以毫秒為單位轉換為格式化字符串,此方法會將您的毫秒轉換為數據格式。

 public static String getTime(long milliseconds)
{

         return DateFormat.format("MMM dd, yyyy", milliseconds).toString();
}

您也可以嘗試DATE FORMATE類以更好地理解。

You can't convert date from one format to other. while you are taking the date take you have take the date which ever format the you want. If you want the date in yyyy-mm-dd. You can get this by using following way.

            java.util.Calendar calc = java.util.Calendar.getInstance();

        int day = calc.get(java.util.Calendar.DATE);
        int month = calc.get(java.util.Calendar.MONTH)+1;
        int year = calc.get(java.util.Calendar.YEAR);

           String currentdate = year +"/"+month +"/"+day ;

試試這個簡單的方法:

        fun getFormattedDate(strDate:String): String {
        try {
            val dateFormat = SimpleDateFormat("dd/mm/yyyy")//old format
            val dateFormat2 = SimpleDateFormat("mm/dd/yyyy")//require new formate
            val objDate = dateFormat.parse(strDate)
            return dateFormat2.format(objDate)
        } catch (e:Exception) {
            return ""
        }
    }

暫無
暫無

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

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