Calendar Constructor Java toString

前端 未结 5 1405
天命终不由人
天命终不由人 2021-01-21 01:30

What I\'m trying to do is pass a date into the Calendar so that it will format the date ready for use with another constructor. So that i can make use of it later using the func

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-21 02:02

    Looks like too much work to me.

    As a user, I'd rather pass a Date and make the contract clear. Provide a convenience method that converts String to Date:

    public class Top {
    
        public static final DateFormat DEFAULT_FORMAT;
    
        static {
            DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MMM-dd");
            DEFAULT_FORMAT.setLenient(false);
        }
    
        public static void main(String [] args) {
        }
    
        public static Date convert(String dateStr) throws ParseException {
            return DEFAULT_FORMAT.parse(dateStr);
        }     
    
        public static String convert(Date d) {
            return DEFAULT_FORMAT.format(d);
        }   
    }
    

提交回复
热议问题