Date and time Java Exercise - Java Programming Course

 Exercise

Date and time

 Objetive

Create a program to display the current date and time with the following format:

Today is 6 of February of 2015. It´s 03:23:12

 Example Code

public class Main
{
	public static void main(String[] args)
	{
		String day = java.time.LocalDateTime.now().getDayOfMonth().toString("00");
		String month = GetMonth(java.time.LocalDateTime.now().getMonthValue());
		int year = java.time.LocalDateTime.now().getYear();
		String time = java.time.LocalDateTime.now().ToLongTimeString();

		System.out.printf("Today is %1$s of %2$s of %3$s. It´s %4$s." + "\r\n", day, month, year, time);
	}

	private static String GetMonth(int numberMonth)
	{
		String[] months = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decenber"};

		return months[numberMonth - 1];
	}
}


Juan A. Ripoll - Programming Tutorials and Courses © 2024 All rights reserved.  Legal Conditions.