search
HomeJavajavaTutorialIn Java, what is the SimpleDateFormat format code?

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
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);
   }
}

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);
   }
}

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

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java中使用Date和SimpleDateFormat类来处理时间的方法及用法介绍Java中使用Date和SimpleDateFormat类来处理时间的方法及用法介绍Apr 21, 2023 pm 03:01 PM

一.介绍java.util包中的Date类表示特定的时间,精确到毫秒。如果要想使用我们的Date类,那么我们必须得引入我们的Date类。Date类直接写入年份是得不到正确的结果的。因为java中Date是从1900年开始算的,所以前面的第一个参数只要填入从1900年后过了多少年就是你想要得到的年份。月需要减1,日可以直接插入。这种方法用的比较少,常用的是第二种方法。这种方法是将一个符合特定格式,比如yyyy-MM-dd,的字符串转化成为Date类型的数据。首先,定义一个Date类型的对象Date

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

java SimpleDateFormat怎么转换局部变量java SimpleDateFormat怎么转换局部变量May 25, 2023 pm 02:16 PM

说明1、将SimpleDateFormat定义为局部变量时,每个线程都独占SimpleDateFormat目标。2、相当于将多线程序改为单线程序程序,因此不存在线程不安全的问题。实例importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;publicclassSimpleDateForm

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools