-1

I currently need to create my own dates (a user has to specify it), but it is not the current date. I want in in the format specified in Joda Time. DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute)

Although any alternative will help. This is my code:

DateTime firstDate = new DateTime(2020,10,9,12,30);
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
  • So what is wrong with that code? It does exactly what you say you want. What would you want differently? – Andreas Oct 09 '20 at 22:44
  • You may look for date picker and time picker components for the user to specify date and time. Not sure there are any that are integrated with Joda-Time, though. – Ole V.V. Oct 10 '20 at 05:09

1 Answers1

3

You can use LocalDateTime which is part of the modern date-time API.

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2020, 10, 9, 12, 30);
        System.out.println(ldt);
    }
}

Output:

2020-10-09T12:30

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

As suggested here, Joda-time is no longer in active development except to keep timezone data up to date. From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

If you are doing it for your 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