Home > Java > javaTutorial > In Java, what is the SimpleDateFormat format code?

In Java, what is the SimpleDateFormat format code?

WBOY
Release: 2023-09-10 22:01:02
forward
1148 people have browsed it

In Java, what is the SimpleDateFormat format code?

java.text.SimpleDateFormat class is used to format and parse strings into dates and parse dates into strings.

Parsing date strings

One of the constructors of this class accepts a String value representing the desired date format and creates a SimpleDateFormat object. Parse/convert a string to a Date object

  • Instantiate this class by passing the required format string.
  • Use parse() to parse date string method.

The following is a list of letters used for formatting with their descriptions and examples -

##GEra IndicatorAD, BCyYear2005, 96YAnniversary2005, 1996 Mediummonths of the year September, September, 09L一Months of the yearSeptember, September, 09wOne Year Anniversary23##WWD##d##Hour of day (1-24)AM/PM (0- 11) Time# Number of hours in ##am/pm (1-12)Minutes of the hoursMinutes and secondszPacific Standard Time, Greenwich Mean Time-0500Time Zone

-06, -0600, -06:00
Date value: Mon Jun 25 00:00:00 IST 2007
Date value: Sun Jan 01 00:00:00 IST 1920
Date value: Sun Jan 01 00:00:00 IST 1989
Copy after login
Example

Letters

Component

Example

p>

Week of the month

3

Day of the year

129

Someday in January

27

##F

Day of the week

5

E

Day of the week (name)

Monday, Monday

u

Day of the week (number)

1

a

AM/PM

Afternoon. AM

H

Hour of day (0-23)

0, 22

##k

1, 12, 24

K

0, 5, 11

h

1, 5, 12

td>米

25

24

##S

Milliseconds

756

##z

Time Zone

Z

Time zone

X

Example

Live Demonstration
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Sample {
   public static void main(String args[]) throws ParseException {  
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy/dd/MM");      
      Date date = formatter.parse("2007/25/06");
      System.out.println("Date value: "+date);    
      formatter = new SimpleDateFormat("y:G");      
      date = formatter.parse("1920:BC");
      System.out.println("Date value: "+date);    
      formatter = new SimpleDateFormat("D-M-Y");      
      date = formatter.parse("25-05-1989");
      System.out.println("Date value: "+date);
   }
}
Copy after login

Output

Live Demonstration

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Sample {
   public static void main(String args[]) throws ParseException {  
      SimpleDateFormat formatter1 = new SimpleDateFormat("HH:mm:ss");      
      Date time1 = formatter1.parse("07:25:30");
      System.out.println("Date value: "+time1);    
      SimpleDateFormat formatter2 = new SimpleDateFormat("EEE:MMM-d:YYYY");      
      Date time2 = formatter2.parse("Sun:Jan-8:2018");
      System.out.println("Date value: "+time2);    
      SimpleDateFormat formatter3 = new SimpleDateFormat("hh 'o''clock' a");      
      Date time3 = formatter3.parse("09 o'clock AM");
      System.out.println("Date value: "+time3);
   }
}
Copy after login

output

Date value: Thu Jan 01 07:25:30 IST 1970
Date value: Sun Dec 31 00:00:00 IST 2017
Date value: Thu Jan 01 09:00:00 IST 1970
Copy after login

The above is the detailed content of In Java, what is the SimpleDateFormat format code?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template