【问题标题】:From timestamp to Date Android从时间戳到日期 Android
【发布时间】:2017-04-29 14:00:20
【问题描述】:

美好的一天,我有时间戳1481709600,我想要这个时间格式Wed, 14 Dec 2016

我正在尝试使用:

private String getDateFromTimeStamp(Integer dt) {
            Date date = new Date (dt);
            return new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy ").format(date);
}

但是当前的输出是Sun Jan 18 05:35:09 GMT+02:00 1970

我觉得日期格式不对,需要用哪一种?

谢谢!

更新

问题是年月日错误,应该是 2016 年 12 月 14 日而不是 1970 年 1 月 18 日

【问题讨论】:

    标签: android date kotlin timestamp


    【解决方案1】:

    问题是您的 timeStamp 以秒为单位,因此将您的 timeStamp 转换为毫秒,然后使用日期格式功能...

    试试这个...

    JAVA

     private String getDate(long time) {
            Date date = new Date(time*1000L); // *1000 is to convert seconds to milliseconds
            SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
            sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
        
            return sdf.format(date);;
        }
    

    科特林

    fun getDate(time:Long):String {
                var date:Date = Date(time*1000L); // *1000 is to convert seconds to milliseconds
                var sdf:SimpleDateFormat  = SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
                sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
        
            return sdf.format(date);
        }
    

    输出:- 像这样

    注意:- EEE 表示为 Day in Week ,MMM 表示为 Month 等等..

    【讨论】:

      【解决方案2】:

      这对我有用:

      val currentDay = Date()
          val day = currentDay.time
          Logger.i("currentDay = $currentDay and day : $day ")
      
          val c = Calendar.getInstance()
          c.timeInMillis = day
          val d = c.time
          val sdf = SimpleDateFormat("dd/MM/yyyy HH:mm")
      
          Logger.i("meDate : ${sdf.format(d)}")
      

      【讨论】:

        【解决方案3】:

        您可以创建一个简单的方法来从时间戳中获取日期,如下所示

        public String getDateCurrentTimeZone(long timestamp) {
                try {
                    Calendar calendar = Calendar.getInstance();
                    TimeZone tz = TimeZone.getDefault();    calendar.setTimeInMillis(timestamp * 1000);
        calendar.add(Calendar.MILLISECOND,
        tz.getOffset(calendar.getTimeInMillis()));
                    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
        Date currenTimeZone = (Date) calendar.getTime();
        return sdf.format(currenTimeZone);
        } catch (Exception e) {
        }
        return "";
        }
        

        【讨论】:

          【解决方案4】:

          如果您阅读this,您可以进行任何组合
          你需要使用EEE, d MMM yyyy

          更新

          SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
          sdf.format(new Date(1481709600));
          

          【讨论】:

            【解决方案5】:

            试试这个方法获取数据。将时间放在 setTimeInMillis 中,而不是 int

            private String getDate(long time) {
                Calendar cal = Calendar.getInstance(Locale.ENGLISH);
                cal.setTimeInMillis(time);
                String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
                return date;
            }
            

            【讨论】:

            • @VLeonovs 1481709600*1000 然后它会为您提供正确的日期。我已经提到过你 setTimeInMillis。
            【解决方案6】:

            试试这个

            private String getDateFromTimeStamp(long time) {
                    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
                    cal.setTimeInMillis(time);
                    String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
                    return date;
                }
            

            【讨论】:

            • @VLeonovs 您传递的时间戳是什么。您所做的错误是,您使用 Integer 值作为参数,该参数应该是 long 数据类型,即 yu 获得 1970 作为年份,尝试上面的代码并恢复如果您得到相同的输出
            【解决方案7】:

            日期格式错误;你必须使用

             new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").format(date);
            

            【讨论】:

            • 用长整数代替整数
            猜你喜欢
            • 2021-01-28
            • 2017-05-31
            • 2013-06-03
            • 2015-04-25
            • 2021-09-26
            • 2014-04-29
            • 2010-10-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多