Java Tutorial/Development/SimpleDateFormat
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 Add AM PM to time using SimpleDateFormat
- 2 Check if a String is a valid date
- 3 Date Formatting and Localization
- 4 Format date in dd/mm/yyyy format
- 5 Format date in mm-dd-yyyy hh:mm:ss format
- 6 Format hour in H (ormat like 0, 1...23.
- 7 Format hour in h (n AM/PM) format like 1, 2..12.
- 8 Format hour in HH (ormat like.23.
- 9 Format hour in hh (n AM/PM) format like.12.
- 10 Format hour in K (n AM/PM) format like 0, 1..11.
- 11 Format hour in k (ormat like 1, 2..24.
- 12 Format hour in KK (ormat like..11.
- 13 Format hour in kk (ormat like.24.
- 14 Format minutes in mm format liketc.
- 15 Format month in M format like 1,2 etc
- 16 Format Month in MM format liketc.
- 17 Format Month in MMM format like Jan, Feb etc.
- 18 Format Month in MMMM format like January, February etc.
- 19 Format seconds in s format like 1,2 etc.
- 20 Format seconds in ss format liketc.
- 21 Format TimeZone in z (General time zone) format like EST.
- 22 Format TimeZone in Z (RFC ormat like -8000.
- 23 Format TimeZone in zzzz format Eastern Standard Time.
- 24 Formatting a Date Using a Custom Format
- 25 Formatting date with full day and month name and show time up to milliseconds with AM/PM
- 26 Formatting day in dd format liketc.
- 27 Formatting day in d format like 1,2 etc
- 28 Formatting day of week in EEEE format like Sunday, Monday etc.
- 29 Formatting day of week using SimpleDateFormat
- 30 Formatting minute in m format like 1,2 etc.
- 31 Format year in yy format liketc
- 32 Format year in yyyy format like etc.
- 33 Full day name: SimpleDateFormat("EEEE")
- 34 Full length of month name: SimpleDateFormat("MMMM")
- 35 Get Today"s Date
- 36 new SimpleDateFormat("a"): The am/pm marker
- 37 new SimpleDateFormat("hh")
- 38 new SimpleDateFormat("HH.mm.ss")
- 39 new SimpleDateFormat("hh:mm:ss a")
- 40 new SimpleDateFormat("HH:mm:ss Z")
- 41 new SimpleDateFormat("H") // The hour (0-23)
- 42 new SimpleDateFormat("mm")
- 43 new SimpleDateFormat("m"): The minutes
- 44 new SimpleDateFormat("ss")
- 45 new SimpleDateFormat("s"): The seconds
- 46 new SimpleDateFormat("Z")
- 47 new SimpleDateFormat("z"): The time zone
- 48 new SimpleDateFormat("zzzz")
- 49 Parsing custom formatted date string into Date object using SimpleDateFormat
- 50 SimpleDateFormat("dd-MMM-yy")
- 51 SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z")
- 52 SimpleDateFormat("MM/dd/yy")
- 53 SimpleDateFormat("MM"): number based month value
- 54 SimpleDateFormat("yyyy")
- 55 The day in week: SimpleDateFormat("E")
- 56 The day number: SimpleDateFormat("d")
- 57 The month: SimpleDateFormat("M")
- 58 Three letter-month value: SimpleDateFormat("MMM")
- 59 Two digits day number: SimpleDateFormat("dd")
Add AM PM to time using SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
String strDateFormat = "HH:mm:ss a";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println(sdf.format(date));
}
}
//10:20:12 AM
Check if a String is a valid date
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Main {
public static boolean isValidDate(String inDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
try {
dateFormat.parse(inDate.trim());
} catch (ParseException pe) {
return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(isValidDate("2004-02-29"));
System.out.println(isValidDate("2005-02-29"));
}
}
/*
true
false
*/
Date Formatting and Localization
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
DateFormat df = DateFormat.getDateInstance();
if (df instanceof SimpleDateFormat) {
SimpleDateFormat sdf = (SimpleDateFormat) df;
System.out.println(sdf.toPattern());
} else {
System.out.println("sorry");
}
}
}
Format date in dd/mm/yyyy format
import java.util.Date;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
String strDate = sdf.format(date);
System.out.println("formatted date in mm/dd/yy : " + strDate);
sdf = new SimpleDateFormat("dd/MM/yyyy");
strDate = sdf.format(date);
System.out.println("formatted date in dd/MM/yyyy : " + strDate);
}
}
Format date in mm-dd-yyyy hh:mm:ss format
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
String strDate = sdf.format(date);
System.out.println("formatted date in mm/dd/yy : " + strDate);
sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
strDate = sdf.format(date);
System.out.println("formatted date in mm-dd-yyyy hh:mm:ss : " + strDate);
}
}
Format hour in H (ormat like 0, 1...23.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("H");
System.out.println("hour in H format : " + sdf.format(date));
}
}
//hour in h format : 11
Format hour in h (n AM/PM) format like 1, 2..12.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
String strDateFormat = "h";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("hour in h format : " + sdf.format(date));
}
}
//hour in h format : 11
Format hour in HH (ormat like.23.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH");
System.out.println("hour in HH format : " + sdf.format(date));
}
}
//hour in h format : 11
Format hour in hh (n AM/PM) format like.12.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh");
System.out.println("hour in hh format : " + sdf.format(date));
}
}
//hour in h format : 11
Format hour in K (n AM/PM) format like 0, 1..11.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("K");
System.out.println("hour in K format : " + sdf.format(date));
}
}
//hour in h format : 11
Format hour in k (ormat like 1, 2..24.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("k");
System.out.println("hour in k format : " + sdf.format(date));
}
}
//hour in h format : 11
Format hour in KK (ormat like..11.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("KK");
System.out.println("hour in KK format : " + sdf.format(date));
}
}
// hour in h format : 11
Format hour in kk (ormat like.24.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("kk");
System.out.println("hour in kk format : " + sdf.format(date));
}
}
//hour in h format : 11
Format minutes in mm format liketc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("mm");
System.out.println("minutes in mm format : " + sdf.format(date));
}
}
// minutes in m format : 35
Format month in M format like 1,2 etc
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("M");
System.out.println("Current Month in M format : " + sdf.format(date));
}
}
//Current Month in M format : 2
Format Month in MM format liketc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM");
System.out.println("Current Month in MM format : " + sdf.format(date)); }
}
//Current Month in M format : 02
Format Month in MMM format like Jan, Feb etc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MMM");
System.out.println("Current Month in MMM format : " + sdf.format(date));
}
}
//Current Month in MMM format : Feb
Format Month in MMMM format like January, February etc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MMMM");
System.out.println("Current Month in MMMM format : " + sdf.format(date));
}
}
//Current Month in MMMM format : February
Format seconds in s format like 1,2 etc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("s");
System.out.println("seconds in s format : " + sdf.format(date));
}
}
//seconds in s format : 40
Format seconds in ss format liketc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("ss");
System.out.println("seconds in ss format : " + sdf.format(date));
}
}
//seconds in ss format : 14
Format TimeZone in z (General time zone) format like EST.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("zzz");
System.out.println("TimeZone in z format : " + sdf.format(date));
}
}
//TimeZone in z format : PST
Format TimeZone in Z (RFC ormat like -8000.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("Z");
System.out.println("TimeZone in Z format : " + sdf.format(date));
}
}
//TimeZone in Z format : -0800
Format TimeZone in zzzz format Eastern Standard Time.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("zzzz");
System.out.println("TimeZone in zzzz format : " + sdf.format(date));
}
}
//TimeZone in zzzz format : Pacific Standard Time
Formatting a Date Using a Custom Format
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("yy");
String s = formatter.format(new Date());
System.out.println(s);
}
}
Formatting date with full day and month name and show time up to milliseconds with AM/PM
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
String today = formatter.format(new Date());
System.out.println("Today : " + today);
}
}
Formatting day in dd format liketc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String strDateFormat = "dd";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current day in dd format : " + sdf.format(new Date()));
}
}
Formatting day in d format like 1,2 etc
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
String strDateFormat = "d";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current day in d format : " + sdf.format(date));
}
}
Formatting day of week in EEEE format like Sunday, Monday etc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String strDateFormat = "EEEE";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current day of week in EEEE format : " + sdf.format(new Date()));
}
}
Formatting day of week using SimpleDateFormat
//Formatting day of week in E format like Sun, Mon etc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
String strDateFormat = "E";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Current day of week in E format : " + sdf.format(date));
}
}
Formatting minute in m format like 1,2 etc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("m");
System.out.println("minutes in m format : " + sdf.format(date));
}
}
//minutes in m format : 35
Format year in yy format liketc
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yy");
System.out.println("Current year in yy format : " + sdf.format(date));
}
}
//Current year in yy format : 09
Format year in yyyy format like etc.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
System.out.println("Current year in yyyy format : " + sdf.format(date));
}
}
//Current year in yyyy format : 2009
Full day name: SimpleDateFormat("EEEE")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("EEEE");
String s = formatter.format(new Date());
System.out.println(s);
}
}
Full length of month name: SimpleDateFormat("MMMM")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("MMMM");
String s = formatter.format(new Date());
System.out.println(s);
}
}
Get Today"s Date
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Main {
public static void main(String[] args) throws Exception {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.format(calendar.getTime()));
}
}
//17/02/2009
new SimpleDateFormat("a"): The am/pm marker
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("a");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("hh")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("hh");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("HH.mm.ss")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("HH.mm.ss");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("hh:mm:ss a")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Date date = new Date();
Format formatter = new SimpleDateFormat("hh:mm:ss a");
String s = formatter.format(date);
System.out.println(s);
}
}
new SimpleDateFormat("HH:mm:ss Z")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("HH:mm:ss Z");
Date date = (Date) formatter.parseObject("22:14:02 -0500");
System.out.println(date);
}
}
new SimpleDateFormat("H") // The hour (0-23)
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("H");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("mm")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("mm");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("m"): The minutes
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("m");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("ss")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("ss");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("s"): The seconds
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("s");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("Z")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("Z");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("z"): The time zone
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("z");
String s = formatter.format(new Date());
System.out.println(s);
}
}
new SimpleDateFormat("zzzz")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("zzzz");
String s = formatter.format(new Date());
System.out.println(s);
}
}
Parsing custom formatted date string into Date object using SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
Date date = sdf.parse("31/12/06");
System.out.println(date);
}
}
//Sun Dec 31 00:00:00 PST 2006
SimpleDateFormat("dd-MMM-yy")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("dd-MMM-yy");
String s = formatter.format(new Date());
System.out.println(s);
}
}
SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
String s = formatter.format(new Date());
}
}
SimpleDateFormat("MM/dd/yy")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Date date = new Date();
Format formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
System.out.println(s);
}
}
SimpleDateFormat("MM"): number based month value
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("MM");
String s = formatter.format(new Date());
System.out.println(s);
}
}
SimpleDateFormat("yyyy")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("yyyy");
String s = formatter.format(new Date());
System.out.println(s);
}
}
The day in week: SimpleDateFormat("E")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("E");
String s = formatter.format(new Date());
System.out.println(s);
}
}
The day number: SimpleDateFormat("d")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("d");
String s = formatter.format(new Date());
System.out.println(s);
}
}
The month: SimpleDateFormat("M")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("M");
String s = formatter.format(new Date());
System.out.println(s);
}
}
Three letter-month value: SimpleDateFormat("MMM")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("MMM");
String s = formatter.format(new Date());
System.out.println(s);
}
}
Two digits day number: SimpleDateFormat("dd")
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws Exception {
Format formatter = new SimpleDateFormat("dd");
String s = formatter.format(new Date());
System.out.println(s);
}
}