JIYIK CN >

Current Location:Home > Learning > PROGRAM > Java >

Java Change Date Format

Author:JIYIK Last Updated:2025/04/14 Views:

There are various options available for converting date string to date format. The methods mentioned below can bring the desired result. Let us understand the various ways from the following code block.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class StringToDateFormat {
  public static void main(String[] args) throws ParseException {
    System.out.print("Way1: ");
    SimpleDateFormat dt = new SimpleDateFormat("yyyyy-MM-dd");
    System.out.print(dt.parse("2021-11-05") + "\n");

    System.out.print("Way2: ");
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss", Locale.ENGLISH);
    System.out.print(formatter.parse("21/JAN/2021 21:35:56") + "\n");

    System.out.print("Way3: ");
    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("EEE, d MMM yyyy", Locale.ENGLISH);
    System.out.print(LocalDate.parse("Wed, 5 May 2021", formatter1) + "\n");

    System.out.print("Way4: ");
    System.out.print(LocalDate.parse("2021-05-31") + "\n");
  }
}

In Way1, an instance of the class is created SimpleDateFormat. It takes a value in the format of the input date string pattern. So, in this way, we input yyyy-MM-ddthe date in the format of . The instantiation also throws some exceptions like NullPointerExceptionand if the parameter is null or illegal IllegalArgumentException. Now using the recently created formatterobject, we initialize a parsemethod. This method takes the date string as input value and returns the data type after parsing 日期. It throws when the given date string and the formatter do not match, or when the internal date string is not parsed ParseException.

In Way2, SimpleDateFormatthe class is used again to create the format that should be entered. But now, the overridden SimpleDateFormatconstructor of is called. The first parameter is Datea string format/pattern. The other is a string that defines a specific geographic region or area Locale. Note:All locales are not allowed in the method. Now, check dd/MMM/yyyy HH:mm: ssthe pattern that displays the month in mmm format. The format implies that the shorthand form of the month is acceptable in mmm form. In addition, the format string may require hours, minutes, and seconds.

In Way3, use DateTimeFormatthe formatter class to format and print date time objects. ofPatternThe formatter method is used to prepare the formatter for the required pattern. Now call LocalDatethe static methods of the formatter class to parse the date. The method is parseused to parse the text and DateTimeFormatteris used to specify the format of the input date text. The method returns LocalDatea datetime instance and is not null. It throws when the text cannot be parsed DateTimeParseException. The format can also take a day name. EEEThe abbreviations represent the same in the formatter.

In Way4, the method, which is LocalDatea static factory method of the class , is called directly parse. This time, no formatter instance or pattern is defined in any way. Now the function takes yyyy-MM-ddan input date string of the form . The specified date string must always represent a valid date and DateTimeFormatter.ISO_LOCAL_DATEbe converted using the format. The method throws an exception when the text cannot be parsed DateTimeParseException.

Below is the output of the code that converts the date string Dateinto .

Way1: Fri Nov 05 00:00:00 IST 2021
Way2: Thu Jan 21 21:35:56 IST 2021
Way3: 2021-05-05
Way4: 2021-05-31

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Calendar date in YYYY-MM-DD format in Java

Publish Date:2025/04/14 Views:72 Category:Java

Java Date encapsulates the current time and date. The Date class does this with the help of two constructors - Date() and Date(long millisec) constructor. We use Date() the constructor to initialize the object with the current time and date

Implementing a min-max heap in Java

Publish Date:2025/04/14 Views:53 Category:Java

In this article, we will implement a max heap and a min heap using PriorityQueue the class. We will also demonstrate inserting and removing elements from the heap. Introduction to Min-Max Heap in Java Heap is a tree-based data structure, wh

Implementing a Min Heap in Java

Publish Date:2025/04/14 Views:197 Category:Java

A min heap is a heap in which every internal node is less than or equal to the value of its child nodes. We will see in the following points how to implement a min heap with and without using a library. Minimal heap implementation in Java w

Increasing the heap space in Java

Publish Date:2025/04/14 Views:190 Category:Java

In Java, the heap space is mainly used for garbage collection and allocating memory for objects. A default heap space is allocated when JVM is installed on our machine, but it may be different. The following points show how we can increase

Detecting EOF in Java

Publish Date:2025/04/14 Views:91 Category:Java

In this tutorial, we will see how to while detect EOF( End OF File ) in Java using a loop. We will also discuss developing a program that continues reading content until it reaches the end of a file. From a programming perspective, EOF is a

Get resource URL and content in Java

Publish Date:2025/04/14 Views:97 Category:Java

getResource() This tutorial will demonstrate how to use the function to get the resource URL and read the resource file in Java . getResource() Use the function to get the resource URL in Java We will use the method in Java getResource() to

Getting the host name in Java

Publish Date:2025/04/14 Views:78 Category:Java

In this tutorial, we will see how to get the IP address and host name using Java API. InetAddress Get the host name in Java using The package java.net contains classes for handling the IP address and host name of the current machine InetAdd

Get the IP address of the current device in Java

Publish Date:2025/04/14 Views:53 Category:Java

An Internet Protocol (IP) address is an identifier for each device connected to a TCP/IP network. This identifier is used to identify and locate nodes in the middle of communication. The IP address format, such as 127.0.0.0, is a human-read

Generate a random double value between 0 and 1 in Java

Publish Date:2025/04/14 Views:153 Category:Java

This article will introduce three methods to generate double random values ​​between 0 and 1 of primitive types. To demonstrate the randomness of the generated values, we will use a loop to generate ten random double values ​​betwee

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial