Android错误“ org.threeten.bp.temporal.UnsupportedTemporalTypeException:不支持的单位:秒”

问题描述

我正在使用'com.jakewharton.threetenabp:threetenabp:1.2.4'库,将较新的功能DateTimeFormatter用于较低的api版本。

我遇到一种情况,我必须首先从JSON响应中转换日期,该格式为“ 2020-07-23T00:00:00.000Z”

然后,我必须获得开始日期和结束日期之间的秒数才能启动计数器。

这是我创建的解决方案:

public static long dateFormat(String start,String end) {
            DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.ENGLISH);
            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyy",Locale.ENGLISH);
            LocalDate startDate = LocalDate.parse(start,inputFormatter);
            LocalDate endDate = LocalDate.parse(end,inputFormatter);
            String start_date = outputFormatter.format(startDate);
            String end_date = outputFormatter.format(endDate);
            LocalDate sDate = LocalDate.parse(start_date,outputFormatter);
            LocalDate eDate = LocalDate.parse(end_date,outputFormatter);
            return ChronoUnit.SECONDS.between(sDate,eDate);
        }

我收到错误“ org.threeten.bp.temporal.UnsupportedTemporalTypeException:不支持的单位:秒”

我正在调用适配器内的方法,我认为这可能是导致问题的原因。

这是我的适配器代码

 public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.offer_pic)
        ImageView offers_pic;
        @BindView(R.id.offer_countdown)
        CountdownView offer_countdown;
       
        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            prefManager = new PrefManager(mContext);

        }

      

        public void onBind(int position) {
            super.onBind(position);
            Doc item = mData.get(position);

            offer_title.setText(item.getTitle());
            offer_short_desc.setText(item.getDescription());
            Glide.with(mContext)
                    .asBitmap()
                    .load(item.getimage())
                    .into(offers_pic);

            
            Log.d("diff1",ViewUtils.dateFormat(item.getStart(),item.getEnd()) + "empty");

        }
    }

是的,我已经在片段中将其初始化 AndroidThreeTen.init(getActivity());

我是这种时间和日期格式的新手。一些帮助将不胜感激。

解决方法

您无需为给定的日期时间字符串创建DateTimeFormatter,因为它已经是Instant#parse使用的格式。另外,您无需将解析的日期时间从Instant转换为其他类型,因为ChronoUnit.SECONDS.between适用于任何Temporal类型。

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(secondsBetween("2020-07-23T00:00:00.000Z","2020-07-23T00:10:20.000Z"));
    }

    public static long secondsBetween(String startDateTime,String endDateTime) {
        return ChronoUnit.SECONDS.between(Instant.parse(startDateTime),Instant.parse(endDateTime));
    }
}

输出:

620

关于您提到的例外的注释: 您试图从seconds的对象中获取LocalDate,该对象仅包含日期部分(即年,月和月中的一天),而没有任何时间部分(即时,分,秒,纳秒)等等。)。如果您尝试使用具有时间成分的类型(例如LocalDateTime),则不会遇到此异常。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...