0

I have a calendar in Android and when I'm passing custom date as a default date to my calendar after setting the time to calendar. when I'm clicking the calendar to get default date I'm getting one previous date. for ex - 02/02/2021. What's wrong I'm doing? how to get date that i passed on calendar? Calendar showing date

 String dbDate = "03/02/2021"; // (dd/MM/yyyy)
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
    
    Date dateObj = null;
    try{
        dateObj = simpleDateFormat.parse(dbDate);
    }catch (Exception e){
        e.printStackTrace();
    }
    calendar.clear();
    calendar.setTime(dateObj);


    long year2021 = calendar.getTimeInMillis();

    CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
    constraintsBuilder.setStart(year2021);


    builder = MaterialDatePicker.Builder.datePicker();
    builder.setTitleText("SELECT A DATE");
    builder.setSelection(year2021);
    builder.setCalendarConstraints(constraintsBuilder.build());
    materialDatePicker = builder.build();
Bhaskar Jha
  • 101
  • 1
  • 8

1 Answers1

1

The legacy date-time API (java.util date-time types and their formatting type, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Solution using the modern API:

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String dbDate = "03/02/2021";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d/M/u", Locale.ENGLISH);
        LocalDate date = LocalDate.parse(dbDate, dtf);

        ZonedDateTime zdtUtc = date.atStartOfDay(ZoneId.of("Etc/UTC"));

        Instant instant = zdtUtc.toInstant();
        long millis = instant.toEpochMilli();
        System.out.println(instant);
        System.out.println(millis);
    }
}

Output:

2021-02-03T00:00:00Z
1612310400000

Now, you can set millis to builder as follows:

builder.setSelection(millis);

For any reason, if you need to convert this object of Instant to an object of java.util.Date, you can do so as follows:

Date utilDate = Date.from(instant);

Learn more about the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Arvind, But when using DateTimeFormatter.ofPattern it says minimum API level to use this is 26, and currently API level is 19 so is there any way to run same in API 19 also? – Bhaskar Jha May 13 '21 at 13:41
  • 2
    Thanks boss you're awesome, i'm using ThreeTenABP in my project now.. – Bhaskar Jha May 13 '21 at 14:22