Get the dd-MM-yyyy format of date in Java

Get the dd-MM-yyyy format of date in Java

In Java, you can format a date into the "dd-MM-yyyy" format using the java.time.LocalDate and java.time.format.DateTimeFormatter classes introduced in Java 8. Here's how you can achieve this:

Using java.time.LocalDate

import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateFormatExample { public static void main(String[] args) { // Get the current date LocalDate currentDate = LocalDate.now(); // Define the date format pattern DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); // Format the date String formattedDate = currentDate.format(formatter); // Print the formatted date System.out.println("Formatted Date: " + formattedDate); } } 

Explanation:

  1. LocalDate: Represents a date without time. LocalDate.now() gets the current date based on the system clock in the default time-zone.

  2. DateTimeFormatter: Used to define the format pattern for the date. DateTimeFormatter.ofPattern("dd-MM-yyyy") specifies that the date should be formatted as day-month-year.

  3. Formatting: The format method of LocalDate takes a DateTimeFormatter object and returns a formatted string representing the date in the specified pattern.

  4. Output: The output will be the current date formatted in the "dd-MM-yyyy" format, for example, "05-07-2024".

Handling Date Input

If you have a specific date that you want to format instead of the current date, you can create a LocalDate object from a string representation and then format it:

// Example with a specific date input String dateInput = "2024-07-05"; LocalDate specificDate = LocalDate.parse(dateInput); // Parse the date string String formattedSpecificDate = specificDate.format(formatter); // Format the date System.out.println("Formatted Specific Date: " + formattedSpecificDate); 

Handling Legacy Date API (Pre-Java 8)

If you're using an older version of Java without java.time, you can use java.text.SimpleDateFormat from the legacy java.text package:

import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class LegacyDateFormatExample { public static void main(String[] args) { // Get the current date Date currentDate = new Date(); // Define the date format pattern DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); // Format the date String formattedDate = formatter.format(currentDate); // Print the formatted date System.out.println("Formatted Date: " + formattedDate); } } 

However, it's recommended to use java.time.LocalDate for new development as it provides a more modern and thread-safe API for handling dates and times in Java applications.

Examples

  1. Java date format dd-MM-yyyy

    • Description: Convert a Date object to a string in dd-MM-yyyy format in Java.
    • Code:
      import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); String formattedDate = formatter.format(date); System.out.println("Formatted Date: " + formattedDate); } } 
    • Explanation: This code snippet uses SimpleDateFormat to format the current date (date) into the specified "dd-MM-yyyy" format (formatter).
  2. Java LocalDate to dd-MM-yyyy string

    • Description: Format a LocalDate object to a string in dd-MM-yyyy format.
    • Code:
      import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class LocalDateExample { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String formattedDate = currentDate.format(formatter); System.out.println("Formatted Date: " + formattedDate); } } 
    • Explanation: Uses DateTimeFormatter from java.time package to format the current date (currentDate) into "dd-MM-yyyy" format.
  3. Java SimpleDateFormat dd-MM-yyyy example

    • Description: Parse a string date in dd-MM-yyyy format into a Date object.
    • Code:
      import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample { public static void main(String[] args) { String dateString = "31-12-2023"; SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); try { Date parsedDate = formatter.parse(dateString); System.out.println("Parsed Date: " + parsedDate); } catch (ParseException e) { e.printStackTrace(); } } } 
    • Explanation: Demonstrates how to parse a string (dateString) in "dd-MM-yyyy" format into a Date object using SimpleDateFormat.
  4. Java date format conversion

    • Description: Convert a date from one format to dd-MM-yyyy format.
    • Code:
      import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatConversion { public static void main(String[] args) { String inputDateString = "2023-12-31"; // Example input date SimpleDateFormat inputFormatter = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat outputFormatter = new SimpleDateFormat("dd-MM-yyyy"); try { Date date = inputFormatter.parse(inputDateString); String formattedDate = outputFormatter.format(date); System.out.println("Formatted Date: " + formattedDate); } catch (ParseException e) { e.printStackTrace(); } } } 
    • Explanation: Converts a date string (inputDateString) from "yyyy-MM-dd" format to "dd-MM-yyyy" format using two instances of SimpleDateFormat.
  5. Java Calendar to dd-MM-yyyy

    • Description: Format a Calendar object to a string in dd-MM-yyyy format.
    • Code:
      import java.text.SimpleDateFormat; import java.util.Calendar; public class CalendarFormatExample { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); String formattedDate = formatter.format(calendar.getTime()); System.out.println("Formatted Date: " + formattedDate); } } 
    • Explanation: Uses Calendar.getInstance() to get the current date and formats it into "dd-MM-yyyy" format using SimpleDateFormat.
  6. Java convert dd-MM-yyyy to yyyy-MM-dd

    • Description: Convert a date string from dd-MM-yyyy format to yyyy-MM-dd format.
    • Code:
      import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateConversionExample { public static void main(String[] args) { String inputDateString = "31-12-2023"; // Example input date SimpleDateFormat inputFormatter = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat outputFormatter = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = inputFormatter.parse(inputDateString); String convertedDate = outputFormatter.format(date); System.out.println("Converted Date: " + convertedDate); } catch (ParseException e) { e.printStackTrace(); } } } 
    • Explanation: Parses a date string (inputDateString) from "dd-MM-yyyy" format into a Date object and then formats it to "yyyy-MM-dd" format.
  7. Java get day-month-year from Date

    • Description: Extract day, month, and year components from a Date object in dd-MM-yyyy format.
    • Code:
      import java.text.SimpleDateFormat; import java.util.Date; public class DateComponentsExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat dayFormatter = new SimpleDateFormat("dd"); SimpleDateFormat monthFormatter = new SimpleDateFormat("MM"); SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy"); String day = dayFormatter.format(date); String month = monthFormatter.format(date); String year = yearFormatter.format(date); System.out.println("Day: " + day); System.out.println("Month: " + month); System.out.println("Year: " + year); } } 
    • Explanation: Uses SimpleDateFormat to extract day, month, and year components (day, month, year) from the current date (date).
  8. Java date to dd-MM-yyyy string

    • Description: Convert a Date object to a string in dd-MM-yyyy format using Java 8's DateTimeFormatter.
    • Code:
      import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class LocalDateExample { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String formattedDate = currentDate.format(formatter); System.out.println("Formatted Date: " + formattedDate); } } 
    • Explanation: Demonstrates the usage of DateTimeFormatter from java.time.format package to format the current date (currentDate) into "dd-MM-yyyy" format.
  9. Java date format dd-MM-yyyy using SimpleDateFormat

    • Description: Use SimpleDateFormat to format a Date object to dd-MM-yyyy format in Java.
    • Code:
      import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); String formattedDate = formatter.format(date); System.out.println("Formatted Date: " + formattedDate); } } 
    • Explanation: Utilizes SimpleDateFormat to format the current date (date) into "dd-MM-yyyy" format (formatter).
  10. Java convert string to dd-MM-yyyy date

    • Description: Parse a string date in dd-MM-yyyy format into a Date object using SimpleDateFormat.
    • Code:
      import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateExample { public static void main(String[] args) { String dateString = "31-12-2023"; SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); try { Date date = formatter.parse(dateString); System.out.println("Parsed Date: " + date); } catch (ParseException e) { e.printStackTrace(); } } } 
    • Explanation: Parses a string date (dateString) in "dd-MM-yyyy" format into a Date object using SimpleDateFormat.

More Tags

gitignore discount plesk binning itext datacolumn codeblocks laravel-passport android-livedata angular-ng-class

More Programming Questions

More Retirement Calculators

More Chemistry Calculators

More Housing Building Calculators

More Biology Calculators