java 日期格式相互转化

1 java日期format 相关的说明

LETTER DATE OR TIME COMPONENT PRESENTATION EXAMPLES
G Era designator Text AD
y Year Year 199696
Y Week year Year 200909
M Month in year (context sensitive) Month JulyJul07
L Month in year (standalone form) Month JulyJul07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text TuesdayTue
u Day number of week (1 = Monday, …, 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard TimePSTGMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08-0800-08:00

 

2 转化示例

  1. import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class DateToStringDemo{
       public static void main(String args[])
       {
           Date todaysDate = new Date();
           DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
           DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
           DateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy");
           DateFormat df4 = new SimpleDateFormat("MM dd, yyyy");
           DateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy");
           DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
           try
           {
               //format() method Formats a Date into a date/time string. 
               String testDateString = df.format(todaysDate);
               System.out.println("String in dd/MM/yyyy format is: " + testDateString);
               String str2 = df2.format(todaysDate);
               System.out.println("String in dd-MM-yyyy HH:mm:ss format is: " + str2);
               String str3 = df3.format(todaysDate);
               System.out.println("String in dd-MMM-yyyy format is: " + str3);
               String str4 = df4.format(todaysDate);
               System.out.println("String in MM dd, yyyy format is: " + str4);
               String str5 = df5.format(todaysDate);
               System.out.println("String in E, MMM dd yyyy format is: " + str5);
               String str6 = df6.format(todaysDate);
               System.out.println("String in E, E, MMM dd yyyy HH:mm:ss format is: " + str6);
           }
           catch (Exception ex ){
              System.out.println(ex);
           }
        }
    }
    //输出
    String in dd/MM/yyyy format is: 02/01/2014
    String in dd-MM-yyyy HH:mm:ss format is: 02-01-2014 22:38:35
    String in dd-MMM-yyyy format is: 02-Jan-2014
    String in MM dd, yyyy format is: 01 02, 2014
    String in E, MMM dd yyyy format is: Thu, Jan 02 2014
    String in E, E, MMM dd yyyy HH:mm:ss format is: Thu, Jan 02 2014 22:38:35

     

  1. import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class StringToDateDemo{
       public static void main(String args[])
       {
           String testDateString = "02/04/2014";
           String testDateString2 = "02-04-2014 23:37:50";
           String testDateString3 = "02-Apr-2014";
           String testDateString4 = "04 02, 2014";
           String testDateString5 = "Thu, Apr 02 2014";
           String testDateString6 = "Thu, Apr 02 2014 23:37:50";
           DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
           DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
           DateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy");
           DateFormat df4 = new SimpleDateFormat("MM dd, yyyy");
           DateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy");
           DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
           try
           {
               //format() method Formats a Date into a date/time string. 
               Date d1 = df.parse(testDateString);
               System.out.println("Date: " + d1);
               System.out.println("Date in dd/MM/yyyy format is: "+df.format(d1));
               Date d2 = df2.parse(testDateString2);
               System.out.println("Date: " + d2);
               System.out.println("Date in dd-MM-yyyy HH:mm:ss format is: "+df2.format(d2));
               Date d3 = df3.parse(testDateString3);
               System.out.println("Date: " + d3);
               System.out.println("Date in dd-MMM-yyyy format is: "+df3.format(d3));
               Date d4 = df4.parse(testDateString4);
               System.out.println("Date: " + d4);
               System.out.println("Date in MM dd, yyyy format is: "+df4.format(d4));
               Date d5 = df5.parse(testDateString5);
               System.out.println("Date: " + d5);
               System.out.println("Date in E, MMM dd yyyy format is: "+df5.format(d5));
               Date d6 = df6.parse(testDateString6);
               System.out.println("Date: " + d6);
               System.out.println("Date in E, E, MMM dd yyyy HH:mm:ss format is: "+df6.format(d6));
           }
           catch (Exception ex ){
              System.out.println(ex);
           }
       }
    }
    //output
    Date: Wed Apr 02 00:00:00 IST 2014
    Date in dd/MM/yyyy format is: 02/04/2014
    Date: Wed Apr 02 23:37:50 IST 2014
    Date in dd-MM-yyyy HH:mm:ss format is: 02-04-2014 23:37:50
    Date: Wed Apr 02 00:00:00 IST 2014
    Date in dd-MMM-yyyy format is: 02-Apr-2014
    Date: Wed Apr 02 00:00:00 IST 2014
    Date in MM dd, yyyy format is: 04 02, 2014
    Date: Wed Apr 02 00:00:00 IST 2014
    Date in E, MMM dd yyyy format is: Wed, Apr 02 2014
    Date: Wed Apr 02 23:37:50 IST 2014
    Date in E, E, MMM dd yyyy HH:mm:ss format is: Wed, Apr 02 2014 23:37:50

     

格式例子:

  1. dd-MM-yy 31-01-12
    dd-MM-yyyy 31-01-2012
    MM-dd-yyyy 01-31-2012
    yyyy-MM-dd 2012-01-31
    yyyy-MM-dd HH:mm:ss 2012-01-31 23:59:59
    yyyy-MM-dd HH:mm:ss.SSS 2012-01-31 23:59:59.999
    yyyy-MM-dd HH:mm:ss.SSSZ 2012-01-31 23:59:59.999+0100
    EEEEE MMMMM yyyy HH:mm:ss.SSSZ Saturday November 2012 10:45:42.720+0100
    
    yyyy-MM-dd'T'HH:mm:ssZZZZ 2017-02-19T22:36:06-0800
    
    dd-MM-yyyy EEEE hh:mm:ss a 22-11-2017 Wednesday 12:16:38 PM

     

原文发表于:https://www.52xintaidu.com/java/397.html

你可能感兴趣的:(java技术)