美文网首页
java日期互转

java日期互转

作者: davidic | 来源:发表于2019-03-05 14:29 被阅读0次
    
    import java.sql.Timestamp;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    /**
     * String Date Calendar之间的转换
     */
    public class DateTransform {
        /**
         * Calendar 转 String
         * @param c Calendar
         * @param dateFormatStr 如"yyyy-MM-dd"
         * @return
         */
        public String calendar2String(Calendar c, String dateFormatStr) {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormatStr);
            return sdf.format(c.getTime());
        }
    
        /**
         * String转Calendar
         * @param dateStr
         * @param dateFormatStr 如"yyyy-MM-dd"
         * @return
         */
        public Calendar string2Calendar(String dateStr, String dateFormatStr) throws ParseException {
            SimpleDateFormat sdf= new SimpleDateFormat(dateFormatStr);
            Date date =sdf.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        }
    
        /**
         * Date转String
         * @param date
         * @param dateFormatStr 如"yyyy-MM-dd"
         * @return
         */
        public String date2String(Date date, String dateFormatStr) {
            SimpleDateFormat sdf= new SimpleDateFormat(dateFormatStr);
            return sdf.format(date);
        }
    
        /**
         * String转Date
         * @param dateStr
         * @param dateFormatStr 如"yyyy-MM-dd"
         * @return
         */
        public Date string2Date(String dateStr, String dateFormatStr) throws ParseException {
            SimpleDateFormat sdf= new SimpleDateFormat(dateFormatStr);
            return sdf.parse(dateStr);
        }
    
        /**
         * Date转Calendar
         * @param date
         * @return
         */
        public Calendar date2Calendar(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        }
    
        /**
         * Calendar转Date
         * @param c
         * @return
         */
        public Date calendar2Date(Calendar c) {
            return c.getTime();
        }
    
        /**
         * String转Timestamp
         * @param dateStr
         * @return
         */
        public Timestamp string2Timestamp(String dateStr) {
            return Timestamp.valueOf(dateStr);
        }
    
        /**
         * Date转Timestamp
         * @param date
         * @param dateFormatStr 如"yyyy-MM-dd"
         * @return
         */
        public Timestamp date2Timestamp(Date date, String dateFormatStr) {
            SimpleDateFormat df = new SimpleDateFormat(dateFormatStr);
            String time = df.format(date);
            return Timestamp.valueOf(time);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:java日期互转

          本文链接:https://www.haomeiwen.com/subject/rvonuqtx.html