【问题标题】:How to change date format coming from database to android如何将来自数据库的日期格式更改为android
【发布时间】:2015-01-04 23:45:30
【问题描述】:

在我的 android 应用程序中,我使用 JSON 解析 mysql 数据库中的数据并在 android listview 中显示。在 mysql 数据库中,日期格式为YYYY-MM-DD,我在 android 中使用 JSON 作为字符串来获取此格式。是否可以在android中将日期格式更改为DD-MM-YYYY而不是数据库..?

【问题讨论】:

    标签: android mysql json date format


    【解决方案1】:

    使用SimpleDateFormat 将日期从一种格式转换为另一种格式:

    SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-DD");
    SimpleDateFormat df2 = new SimpleDateFormat("DD-MM-YYYY");
    try{
        Date d = df1.parse(DateString);
        System.out.println("new date format " + df2.format(d));
    }catch(Exception e){
       e.printStackTrace();
    }
    

    【讨论】:

      【解决方案2】:

      是的,是的。使用SimpleDateFormat

      String inputPattern = "yyyy-MM-dd";
      String outputPattern = "dd-MM-yyyy";
      SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
      SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
      
      Date date = null;
      String result = null; 
      
      try {
          date = inputFormat.parse(time);
          result = outputFormat.format(date);
      } catch (ParseException e) {
          e.printStackTrace();
      }
      
      //use the result variable as you wish
      

      【讨论】:

      • 请检查您的输出格式是否错误,按照要求的问题 dd-MM-yyyy 并且您使用的是 dd-MMM-yyyy。
      • @HareshChhelana 谢谢 :)
      【解决方案3】:

      这将是我需要的答案:D

         String well=String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", 
         java.util.Locale.getDefault()).format(new java.util.Date()));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        • 2019-09-16
        • 1970-01-01
        • 1970-01-01
        • 2023-02-03
        相关资源
        最近更新 更多