Use various format
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main{
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("EEE, MMM d, ''yy");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("h:mm a");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("hh 'o''clock' a, zzzz" );
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("K:mm a, z");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("yyMMddHHmmssZ");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
System.out.println(sdf.format(date));
}
}
The output:
2010.10.30 AD at 10:36:09 PDT
Sat, Oct 30, '10
10:36 AM
10 o'clock AM, Pacific Daylight Time
10:36 AM, PDT
02010.October.30 AD 10:36 AM
Sat, 30 Oct 2010 10:36:09 -0700
101030103609-0700
2010-10-30T10:36:09.656-0700
MM/dd/yy
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);
}
}
The output:
formatted date in mm/dd/yy : 11/14/10
dd/MM/yyyy
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("dd/MM/yyyy");
String strDate = sdf.format(date);
System.out.println("formatted date in dd/MM/yyyy : " + strDate);
}
}
The output:
formatted date in dd/MM/yyyy : 14/11/2010
MM-dd-yyyy hh:mm:ss
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-yyyy hh:mm:ss");
String strDate = sdf.format(date);
System.out.println("formatted date in MM-dd-yyyy hh:mm:ss:" + strDate);
}
}
The output:
formatted date in MM-dd-yyyy hh:mm:ss:11-14-2010 10:02:55
Home
Java Book
Essential Classes
Java Book
Essential Classes
SimpleDateFormat:
- SimpleDateFormat class introduction
- SimpleDateFormat formats date, day,
- Add AM PM to time format using SimpleDateFormat
- Formatting date in default formats using DateFormat
- Formatting day using SimpleDateFormat
- Formatting day of week using SimpleDateFormat
- Formatting hour using SimpleDateFormat
- Formatting hour using SimpleDateFormat: k
- Formatting Minutes using SimpleDateFormat
- Formatting month using SimpleDateFormat
- Formatting seconds using SimpleDateFormat
- Formatting TimeZone using SimpleDateFormat
- Formatting year using SimpleDateFormat
- Use various format
- Convert date string from one format to another format using SimpleDateFormat