1

I have a Calender object that is chosen by user. I need to compare this object (or convert) with LocalDate. Is there any way to do that? I am getting calendar object by that :

        val calendar = Calendar.getInstance()
        val calendar2 = Calendar.getInstance()
        val pickDate = DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
            calendar.set(Calendar.YEAR,year)
            calendar.set(Calendar.MONTH,month)
            calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth)

            update(calendar)

        }
        val pickTime = TimePickerDialog.OnTimeSetListener { view, hour, minute ->
            calendar2.set(Calendar.HOUR_OF_DAY,hour)
            calendar2.set(Calendar.MINUTE,minute)
            update2(calendar2)
        }

        datePicker.setOnClickListener {
            DatePickerDialog(this.requireContext(),pickDate,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show()



        }
        timePicker.setOnClickListener {
            TimePickerDialog(this.requireContext(),pickTime,calendar2.get(Calendar.HOUR_OF_DAY),calendar2.get(Calendar.MINUTE),true).show()
        }

    }

    @SuppressLint("SetTextI18n")
    fun update(calendar : Calendar)  {
        val format = SimpleDateFormat("dd-MM-yyyy",Locale.US).format(calendar.time)
        showDate.text = format




    }
    fun update2(calendar : Calendar)  {
        val format2 = SimpleDateFormat("HH:mm",Locale.US).format(calendar.time)
        showDate2.text = format2

    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • show some code pls, at least something you've done to get it done – Wale Jul 30 '21 at 13:27
  • Welcome to StackOverflow! Please refer to the guide on [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and when needed, how to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so people can try to help you with a problem. You're often expected to do your own homework and research before asking a question. – Martin Marconcini Jul 30 '21 at 13:28

2 Answers2

1

The conversion is straightforward when you know how. I will have to trust you to convert my Java to Kotlin.

    GregorianCalendar calendar = new GregorianCalendar();
    // Fill year, month, dayOfMonth into the following line
    calendar.set(2021, Calendar.JULY, 31);
    
    LocalDate date = calendar.toZonedDateTime().toLocalDate();
    
    System.out.println("Converted to LocalDate: " + date);

Output from this snippet is:

Converted to LocalDate: 2021-07-31

I have assumed that you want to use the Gregorian calendar always. Just as LocalDate always uses the proleptic Gregorian calendar.

Edit: the still better and also simpler option is to do away with the Calendar class altogether and just use LocalDate throughout:

    int year = 2021;
    int month = Calendar.JULY; // For demonstration; don’t use Calendar in your code
    int dayOfMonth = 31;
    
    LocalDate date = LocalDate.of(year, month + 1, dayOfMonth);
    
    System.out.println("Created as LocalDate: " + date);

Created as LocalDate: 2021-07-31

Downside is the funny adding 1 to the month number because your date picker numbers months from 0 = January while LocalDate sensibly numbers from 1.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

The first step to getting LocalDate out of a Calendar object is to convert it into an Instant which can be converted to other java.time types.

Demo:

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        // A sample Date-Time
        Calendar calendar = Calendar.getInstance();
        calendar.set(2021, 6, 30, 14, 50); // Note: Month has 0-based index

        Instant instant = calendar.toInstant();
        System.out.println(instant);

        // Change JVM's default ZoneId, ZoneId.systemDefault() with the applicable
        // ZoneId e.g. ZoneId.of("America/New_York")
        LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        System.out.println(ldt);

        LocalDate date = ldt.toLocalDate();
        System.out.println(date);
    }
}

Output:

2021-07-30T13:50:47.528Z
2021-07-30T14:50:47.528
2021-07-30

ONLINE DEMO

An Instant represents an instantaneous point on the timeline, normally represented in UTC time. The Z in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

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