androiddatetimethreetenbpjava.time.instantdatetimeformatter

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


I am using the 'com.jakewharton.threetenabp:threetenabp:1.2.4' library for using Newer features DateTimeFormatter for lower apis version.

I have a situation where I have to first convert the the date from the JSON response which is in "2020-07-23T00:00:00.000Z" this format.

Then i have to get the seconds between the start & end date to start a counter.

Here is the solution I created:

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

I am getting the error "org.threeten.bp.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds"

I am calling the method inside the adapter, i think that might be causing the issue.

Here is my adapter Code:

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

        }
    }

And yes I have initialized it in the fragment like AndroidThreeTen.init(getActivity());

I am new to this sort of time & date formatting. Some help will be really appreciated.


Solution

  • You do not need to create a DateTimeFormatter for the given date-time string as it's already in the format used by Instant#parse. Also, you do not need to convert the parsed date-time from Instant to some other type as ChronoUnit.SECONDS.between works for any Temporal type.

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

    Output:

    620
    

    A note on the exception you have mentioned: You tried to get seconds from an object of LocalDate which contains just the date parts (i.e. year, month and the day of the month) and not any time part (i.e. hour, minute, second, nanoseconds etc.). Had you tried using a type which has time component (e.g. LocalDateTime), you would not got this exception.