1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/* Formatting date in custom formats using SimpleDateFormat This example shows how to format date and time in custom formats using Java SimpleDateFormat class. */ import java.util.Date; import java.text.SimpleDateFormat; public class FormattingDateInCustomFormat { public static void main(String[] args) { //create Date object Date date = new Date(); //create object of SimpleDateFormat class with custom format SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy"); String strDate = sdf.format(date); System.out.println("formatted date in mm/dd/yy : " + strDate); //format date in dd/mm/yyyy format 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 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); } } /* Typical output would be formatted date in mm/dd/yy : 12/27/07 formatted date in dd/MM/yyyy : 27/12/2007 formatted date in mm-dd-yyyy hh:mm:ss : 12-27-2007 06:44:26 */ |
Add Comment