java轉換日期格式為 RFC1123


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

public class GmtDate {
    public static void main(String[] args){
        
        System.out.println(new Date());
        
        //本地日期格式.
        String rfc1123_1 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",Locale.US).format(new Date());
        System.out.println("rfc1123_1 = " + rfc1123_1);
        
        //錯誤
        String rfc1123_2 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'",Locale.US).format(new Date());
        System.out.println("rfc1123_2 = " + rfc1123_2);

        //正確, 推薦使用.
        SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",Locale.US);
        sdf3.setTimeZone(TimeZone.getTimeZone("GMT"));
        String rfc1123_3 = sdf3.format(new Date());
        System.out.println("rfc1123_3 = "+rfc1123_3);
        
        //正確.
        SimpleDateFormat sdf4 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'",Locale.US);
        sdf4.setTimeZone(TimeZone.getTimeZone("GMT"));
        String rfc1123_4 = sdf4.format(new Date());
        System.out.println("rfc1123_4 = " + rfc1123_4);
    }
}

輸出的結果

Tue Oct 30 15:33:48 CST 2018
rfc1123_1 = Tue, 30 Oct 2018 15:33:48 CST
rfc1123_2 = Tue, 30 Oct 2018 15:33:48 GMT
rfc1123_3 = Tue, 30 Oct 2018 07:33:48 GMT
rfc1123_4 = Tue, 30 Oct 2018 07:33:48 GMT

 ****************************************************

對應的命令

date=`env LANG="en_US.UTF-8" date -u "+%a, %d %b %Y %H:%M:%S GMT"`

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM