字符串類型的"Sun Oct 24 20:49:20 CST 2021"轉換為Date格式


oracle數據庫,定義的TIMESTAMP類型,從數據庫中取出作時間對比的時候,發現取出是Sun Oct 24 20:49:20 CST 2021這種格式,需要轉換為常見的日期格式作對比

 public static void main( String[] args )
    {
        String t1 = "Sun Oct 24 20:49:20 CST 2021";
        String t2 = "Sun Oct 24 20:49:21 CST 2021";

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = sdf.parse(t1);
            d2 = sdf.parse(t2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(d1) + "-----");
        System.out.println(sdf.format(d2) + "-----");
        if (sdf.format(d1).compareTo(sdf.format(d2)) == 0) {
            System.out.println("時間相等");
        }
    }


免責聲明!

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



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