[英]“EEE MMM dd HH:mm:ss ZZZ yyyy” date format to java.sql.Date
[英]Parsing custom date with MM/dd/yyyy hh:mm:ss zzz format in android
我有一個字符串,表示格式為MM/dd/yyyy hh:mm:ss
,如下所示:
"03/26/2014 17:32:25 IST"<BR>
當我將字符串解析為日期時:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss zzz");
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getDefault());
cal.setTime(sdf.parse("03/26/2014 17:32:25 IST"));
String output = sdf.parse(sdf.format(cal.getTime()));
我嘗試在SimpleDateFormat中添加Locale.getDefault()
但它不起作用。
任何幫助將不勝感激。
第一個錯誤:您嘗試在String
(compile-error)上設置java.util.Date
(parse-method的結果)。
第二個錯誤:您應該使用模式符號HH而不是hh(根據17小時的小時輸入,二十四小時時鍾)。
第三個錯誤:在格式對象上設置時區,而不是在日歷上(有希望對應於時區IST - 您在以色列或印度)。
更新: “IST”似乎不是Android設備上的已知時區名稱。 Google-Android的動機可能是為了避免含糊不清的名稱(“以色列標准時間”或“印度標准時間”),因此Android在其資源庫中有其他不同的名稱。 您可以嘗試文本預處理,如下所示:
if (timestampText.endsWith(" IST")) {
timestampText = timestampText.substring(0, timestampText.length() - 4);
}
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
java.util.Date d = sdf.parse(timestampText);
還要檢查方法DateFormatSymbols.getInstance().getZoneStrings()
的輸出,以查看Android是否需要另一個時區名稱而不是“IST”(在Java-VM上更廣泛)。
Try Below code you need to assign output into Date data type :
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss zzz");
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getDefault());
cal.setTime(sdf.parse("03/26/2014 17:32:25 IST"));
java.util.Date output = sdf.parse(sdf.format(cal.getTime()));
嗯,這樣的? 確保使用正確的進口..
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss zzz");
Date parse = null;
Calendar calendar = Calendar.getInstance();
try {
parse = sdf.parse("03/26/2014 17:32:25 IST");
calendar.setTime(parse);
} catch (ParseException e) {
e.printStackTrace();
}
Toast.makeText(this, calendar.getTime() + "", Toast.LENGTH_SHORT).show();
}
}
嘗試以下代碼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String date_parse="2015-04-09 05:06:14";
try{
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdfSource.parse(date_parse);
SimpleDateFormat sdfDestination = new SimpleDateFormat("MMMM dd,yyyy hh:mm a");
date_parse = sdfDestination.format(date);
System.out.println("Date is converted from dd/MM/yy format to MMMM dd,yyyy hh:mm a");
System.out.println("Converted date is : " + date_parse);
} catch(Exception e){
System.out.println(e.getMessage());
}
}
輸出:日期從dd / MM / yy格式轉換為MMMM dd,yyyy hh:mm a轉換日期為:April 09,2015 05:06 AM
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.