在 Kotlin 中比较 Calendar 和 LocalDate

Compare Calendar and LocalDate in Kotlin

我有一个由用户选择的 Calender 对象。我需要将此对象(或转换)与 LocalDate 进行比较。有什么办法吗?我通过那个获得日历对象:

        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

    }
}

Calendar 对象中获取 LocalDate 的第一步是将其转换为可以转换为其他 java.time 类型的 Instant

演示:

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

输出:

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

ONLINE DEMO

零时区偏移的 Instant represents an instantaneous point on the timeline, normally represented in UTC time. The Z in the output is the timezone designator。它代表祖鲁语并指定 Etc/UTC 时区(时区偏移量为 +00:00 小时)。

详细了解 modern Date-Time API* from Trail: Date Time


* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用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

当您知道如何操作时,转换很简单。我必须相信你能将我的 Java 转换为 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);

此片段的输出是:

Converted to LocalDate: 2021-07-31

我假设您想始终使用公历。正如 LocalDate 总是使用公历。

编辑:更好更简单的选择是完全取消 Calendar class 并始终使用 LocalDate

    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

缺点是在月份数字上加 1 很有趣,因为您的日期选择器从 0 = 一月开始对月份进行编号,而 LocalDate 明智地从 1 开始编号。