Calendar to date kotlin

Calendar View App in Android with Kotlin

Calendar View is seen in most travel booking applications in which the user has to select the date of the journey. For the selection of the date, this view is used.

Calendar View App in Android with Kotlin

Calendar View is seen in most travel booking applications in which the user has to select the date of the journey. For the selection of the date, this view is used. In this article, we will take a look at How to implement calendar view within our Android application using Kotlin. A sample video is given below to get an idea about what we are going to do in this article.

Note : If you want to implement Calendar View in Android applications using Java. Check out the following article: Calendar View application in Android using Java.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio . Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

Читайте также:  Css and div example

XML

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin

Now run your application to see the output of it.

How do i get the days of the week using kotlin in android, It is because add () method returns nothing. If you want to get day of month, try something like that: val calendar: Calendar = Calendar.getInstance () calendar.add (Calendar.DAY_OF_MONTH, Calendar.TUESDAY) val day= calendar.get (Calendar.DAY_OF_MONTH) binding.txtNumeroTerca.text = …

How to convert Calendar to Date in Kotlin?

I am trying to add one month to a date with Calendar.getInstance() but I can’t figure out why there is error «java.lang.ClassCastException : java.util.Date cannot be cast to java.lang.Number» when trying to get the Calendar to a Date object.

Here’s the source code that I am using :

 val date = Date() val cal = Calendar.getInstance() cal.time = date cal.add(Calendar.MONTH, 1) val datePlusOneMonth: Date = cal.time 

Thank you Sergey. The code runs ok. But I have been disappointed because I added a useless line of code in order to debug and I had put a breakpoint on it :

val datePlusOneMonth: Date = cal.time val ok = false 

And I had put the breakpoint on the «val ok = false» and the debugger never stopped on «val ok = false» because «ok» was never used.

Then to have the debugger stop on «val ok = false» I had to do the following :

val datePlusOneMonth: Date = cal.time val ok = false val ok2 = ok 

And then I could add the breakpoing on «val ok = false» and yes the code runs well.

val datePlusOneMonth = Calendar.getInstance().run

How to get current local date and time in Kotlin, val date = Calendar.getInstance().time val formatter = SimpleDateFormat.getDateTimeInstance() //or use getDateInstance() val formatedDate = formatter.format(date) You can use your own pattern as well, e.g.

Compare Calendar and LocalDate in Kotlin

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 >> 

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:

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

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.

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.

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 

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.

Datetime — Kotlin: Getting the difference betweeen two, Bookmark this question. Show activity on this post. Sorry if similar questions have been asked too many times, but it seems that there’s one or more issues with every answer I find. I have a date in the form of a String: Ex.: «04112005». This is a date. 4th of November, 2005.

Find in the last day of the month using calendar on Android

I am trying to change the last day allowed in my calendar Widget based on the current date or fix date.

The code below is using a date as a default start date and I need to allow the user to select between this date and date + 3 months later.

val maxDay = Calendar.getInstance() setSelectedDate(currentShipDate.toDate()) maxDay.add(Calendar.MONTH, 3) state().edit() .setMinimumDate(minDay.time) .setMaximumDate(maxDay.time) .commit() 

The maxDay is now set to 3 months later using the currentShipDate . However, I am looking to allow the user to also select a date until the end of the 3rd months.

For example, if the ship happened in 21st July, you can change the shipping date til 31st October (instead of 21st October)

For example, if the ship happened in 10th July, you can change the shipping date til 31st October (instead of 10st October)

  1. Instead of adding 3 months, add 4 —> maxDay.add(Calendar.MONTH, 4)
  2. Set the day of the month to 1 —> maxDay.set(Calendar.DAY_OF_MONTH, 1)
  3. Subtract one day —> maxDay.add(Calendar.DAY_OF_MONTH, -1)

This will give you the last day of the third month.

Java — Compare Calendar and LocalDate in 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 …

Источник

Оцените статью