Skip to content

DevWorkbench

  • Home
  • Links
  • About
  • Toggle search form

Java Examples – Date Parsing & Formatting

Posted on October 9, 2024October 9, 2024 By jeepdogjake

There’s many different ways to parse and format dates in Java. Here’s a simple example of just one way:

package pkgDateFormatter;

import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateFormatter {

public static void main(String[] args) {

    String string = "06/01/2024";
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
    Date date = null;
    try {
        date = sdf.parse(string);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    System.out.println(date); // output: Sat Jun 01 00:00:00 EDT 2024

    sdf.applyPattern("yyyy-MM-dd");
    System.out.println(sdf.format(date)); // Sat Jan 02 00:00:00 GMT 2010

}

}

All available date formats from the javadoc:

SymbolMeaningPresentationExamples
GeratextAD; Anno Domini; A
uyearyear2004; 04
yyear-of-erayear2004; 04
Dday-of-yearnumber189
M/Lmonth-of-yearnumber/text7; 07; Jul; July; J
dday-of-monthnumber10
Q/qquarter-of-yearnumber/text3; 03; Q3; 3rd quarter
Yweek-based-yearyear1996; 96
wweek-of-week-based-yearnumber27
Wweek-of-monthnumber4
Eday-of-weektextTue; Tuesday; T
e/clocalized day-of-weeknumber/text2; 02; Tue; Tuesday; T
Fweek-of-monthnumber3
aam-pm-of-daytextPM
hclock-hour-of-am-pm (1-12)number12
Khour-of-am-pm (0-11)number0
kclock-hour-of-am-pm (1-24)number0
Hhour-of-day (0-23)number0
mminute-of-hournumber30
ssecond-of-minutenumber55
Sfraction-of-secondfraction978
Amilli-of-daynumber1234
nnano-of-secondnumber987654321
Nnano-of-daynumber1234000000
Vtime-zone IDzone-idAmerica/Los_Angeles; Z; -08:30
ztime-zone namezone-namePacific Standard Time; PST
Olocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
Xzone-offset ‘Z’ for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
xzone-offsetoffset-x+0000; -08; -0830; -08:30; -083015; -08:30:15;
Zzone-offsetoffset-Z+0000; -0800; -08:00;

Note that the patterns are case sensitive.

Programming Examples

Post navigation

Previous Post: Java Examples – Screen I/O
Next Post: XSLT Example – Merging Two Documents

Related Posts

XSLT Example – Merging Two Documents Programming Examples
XSLT Example – “Joining” Different Nodes in One Document Programming Examples
Java Examples – Screen I/O Programming Examples

Recent Posts

  • XSLT Example – “Joining” Different Nodes in One Document
  • XSLT Example – Merging Two Documents
  • Java Examples – Date Parsing & Formatting
  • Java Examples – Screen I/O
  • How to reset Ctrl-End in Excel

Copyright © 2026 DevWorkbench.

Powered by PressBook Masonry Dark