字符串类型的"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("时间相等");
        }
    }
作者:山丘!

-------------------------------------------

你闻讯而来,我大喜过望,我在这等你,你又在哪呢?喜欢的话加一个“关注”呗!

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

原文地址:https://www.cnblogs.com/mengd/p/15458460.html