Android 错误 "org.threeten.bp.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds"

Android error "org.threeten.bp.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds"

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

我有一种情况,我必须首先从 "2020-07-23T00:00:00.000Z" 中的 JSON 响应转换日期] 这种格式。

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

这是我创建的解决方案:

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());

我不熟悉这种时间和日期格式。一些帮助将不胜感激。

您不需要为给定的 date-time 字符串创建 DateTimeFormatter,因为它已经采用 Instant#parse 使用的格式。此外,您不需要将解析的 date-time 从 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

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