如何将datetime字符串转换为另一种日期时间格式?

时间:2015-08-20 14:06:05

标签: android date

我在sqlite中有一个列我将当前日期时间保存为字符串现在我的问题是,我无法格式化,如下所示:

此字符串:2015-08-20 18:55:55 pm

此字符串:2015年8月20日06:55:55

谢谢你/

4 个答案:

答案 0 :(得分:2)

这样做

String dateTxt = "2015-08-20 18:55:55 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");
Date date = null;
try 
{
    date = sdf.parse(dateTxt);
}
catch(Exception ex)
{
    ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMMM-yyyy hh:mm:ss aaa");
String newFormat = formatter.format(date);

答案 1 :(得分:1)

使用像

这样的东西
String value = "2015-08-20 18:55:55 pm";
String result = "";
Date date = null;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");
try{
  date = dateFormat.parse(value);
}
 catch(Exception e)
{
   e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat printFormat = new SimpleDateFormat("dd-MMMM-yyyy hh:mm:ss aaa");
result = printFormat.format(calendar.getTime());

答案 2 :(得分:0)

        DateFormat fullDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aaa", Locale.US);
        Date fullDate = fullDateFormat.parse(longDate);  
        resultdate= new SimpleDateFormat("dd-MMM-yyyy hh:mm:sss aaa", Locale.US).format(fullDate);

答案 3 :(得分:0)

只需使用此方法

String pdate = FormatDate(pickdate); // 7:28:2015

 public static String FormatDateReverse(String d){
    //3 September 2014
    SimpleDateFormat fromUser = new SimpleDateFormat("dd MMMM yyyy", Locale.US);
    SimpleDateFormat myFormat = new SimpleDateFormat("MM:dd:yyyy");
    String reformattedStr="";
    try {
    reformattedStr = myFormat.format(fromUser.parse(d));
        Log.d("Date=",reformattedStr);
    } 
    catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     return reformattedStr;
    }
相关问题