【问题标题】:Android Convert Ruby on Rails Timestamp to Date TimeAndroid 将 Ruby on Rails 时间戳转换为日期时间
【发布时间】:2016-08-23 04:45:21
【问题描述】:

我正在构建一个 Android 应用程序,从 Ruby On Rails 框架中构建的 API 获取数据。
这是我的时间戳字符串
2000-01-01T12:00:00.000Z

我需要将其转换为可读的日期和时间格式,即“2000 年 1 月 1 日星期六 12:00 am”

【问题讨论】:

    标签: java android timestamp


    【解决方案1】:

    你可以试试这个:

    public static String convertDate(String oldDateString){
        String format = "yyyy-MM-dd'T'HH:mm:ss.sssZZZZ";
    
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(oldDateString);
        } catch (ParseException e) {
            // handle exception here !
        }
    
        String newFormatString = "MMMM dd, yyyy 'at' HH:mm a";
    
        SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
    
    
    
        String newDateString = newFormatter.format(date);
    
        return newDateString;
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我尝试了@real19 的答案,但它给了我错误。 我当时就来了:

        public static String convertDate(String oldDateString){
          String format = "yyyy-MM-dd'T'HH:mm:ss.sss";
      
          SimpleDateFormat sdf = new SimpleDateFormat(format);
          Date date = null;
          try {
              date = sdf.parse(oldDateString);
          } catch (ParseException e) {
              // handle exception here !
              e.printStackTrace();
          }
      
          String newFormatString = "EEEE MMMM dd, yyyy 'at' HH:mm a";
      
          SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
      
          String newDateString = newFormatter.format(date);
      
          return newDateString;
      }
      

      格式化给了我jeudi décembre 22, 2016 at 16:42 PM,但你可以调整你的 DateFormat

      【讨论】:

        猜你喜欢
        • 2010-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-25
        • 2022-01-08
        相关资源
        最近更新 更多