In this source code example, we show you how to print the current time in the day, month, and year format in Java.
Print current time in a day, month, and year format in Java
package com.ramesh.java8.datetime;
import java.time.LocalDate;
/**
* Useful Java8DateUtiliy Methods
* @author javaguides.net
*
*/
public final class Java8DateUtility {
/**
* Print current time in day,month and year format.
*/
public static void printCurrentDayMonthAndYear() {
LocalDate today = LocalDate.now();
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
System.out.printf("Year : %d Month : %d Day : %d \t %n", year, month, day);
}
}
JUnit test case
package com.ramesh.java8.datetime;
import org.junit.Test;
/**
* JUnit test cases for Java8DateUtiliy Methods
* @author javaguides.net
*
*/
public class Java8DateUtilityTest {
@Test
public void printCurrentDayMonthAndYearTest() {
Java8DateUtility.printCurrentDayMonthAndYear();
}
}
Run the JUnit test cases will print the output:
Year : 2018 Month : 7 Day : 21