简体   繁体   中英

How to set String date to calendar in Java

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? 日历显示日期

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM