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 -
Letters |
Component |
Example |
Era Indicator | AD, BC | |
Year | 2005, 96 | |
Anniversary | 2005, 1996 | |
months of the year | September, September, 09 | |
一Months of the year | September, September, 09 | |
One Year Anniversary | 23 | |
| ||
p> Week of the month |
3 |
|
Day of the year |
129 |
##d |
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 | # Number of hours in ##am/pm (1-12)
1, 5, 12 | td>米 |
Minutes of the hour |
25 | s | Minutes and seconds | 24 | ##S |
Milliseconds |
756 | ##z | z |
Time Zone | ||
|
Z | Time zone |
X | Time Zone||
Example |
Live Demonstrationimport 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 |
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);
}
}
outputDate 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!

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
