How to convert java.util.Date to java.sql.Date?

后端 未结 18 2663
梦如初夏
梦如初夏 2020-11-22 01:44

I am trying to use a java.util.Date as input and then creating a query with it - so I need a java.sql.Date.

I was surprised to find that it couldn\'t do the conver

18条回答
  •  感动是毒
    2020-11-22 02:46

    Method for comparing 2 dates (util.date or sql.date)

     public static boolean isSameDay(Date a, Date b) {
        Calendar calA = new GregorianCalendar();
        calA.setTime(a);
    
        Calendar calB = new GregorianCalendar();
        calB.setTime(b);
    
        final int yearA = calA.get(Calendar.YEAR);
        final int monthA = calA.get(Calendar.MONTH);
        final int dayA = calA.get(Calendar.DAY_OF_YEAR);
    
        final int yearB = calB.get(Calendar.YEAR);
        final int monthB = calB.get(Calendar.MONTH);
        final int dayB = calB.get(Calendar.DAY_OF_YEAR);
    
        return yearA == yearB && monthA == monthB && dayA == dayB;
    }
    

提交回复
热议问题