To get minutes from the local date-time, we can use the getMinute() built-in method of the Java LocalDateTime class.
Let's see the code:
/*
* Code example to get minutes from localdatetime in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// String date is given
String strDate = "2022-03-14T17:28:13.048999208";
// parse the string date into locale date time
LocalDateTime date = LocalDateTime.parse(strDate);
// Displaying date and time
System.out.println("Date : "+date);
// Get minutes from the date
int minutes = date.getMinute();
// Display result
System.out.println("Minutes : "+minutes);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
Minutes: 28
In the above code, we first parsed the String date to the LocalDateTime object by using the parse() method, then we applied the method.
If you already have locadatetime object, then you don't need to parse it.
You can directly call the getMinute() method.
Now, let's have a look at this method signature:
public int getMinute()
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns the minute-of-hour from 0 to 59 of the date-time object.
Parameters: No parameter is required.
Exceptions: No exception.
Version: Since 1.8
Let's see one more example to get the minutes from date-time.
Here, we are getting the minutes from the current date-time and for that first, we used the now() method that returns the current date-time instance.
After that, we used the getMinute() method to get the minutes.
/*
* Code example to get minutes from localdatetime in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// Current date and time
LocalDateTime date = LocalDateTime.now();
// Displaying date and time
System.out.println("Date : "+date);
// Get minutes from the date
int minutes = date.getMinute();
// Display result
System.out.println("Minutes : "+minutes);
}
}
Output:
Date : 2022-03-16T11:54:47.182617221
Minutes : 54