How can I utilize SimpleDateFormat with Calendar?

2019-01-21 15:25发布

I've got GregorianCalendar instances and need to use SimpleDateFormat (or maybe something that can be used with calendar but that provides required #fromat() feature) to get needed output. Please, suggest work arounds as good as permanent solutions.

4条回答
混吃等死
2楼-- · 2019-01-21 16:17

Simply call calendar.getTime() and pass the resulting Date object to the format method.

查看更多
相关推荐>>
3楼-- · 2019-01-21 16:19

Try this:

Calendar cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
dateFormat.setTimeZone(cal.getTimeZone());
System.out.println(dateFormat.format(cal.getTime()));
查看更多
Evening l夕情丶
4楼-- · 2019-01-21 16:27

Calendar.getTime() returns a Date which can be used with SimpleDateFormat.

查看更多
仙女界的扛把子
5楼-- · 2019-01-21 16:28

eQui's answer is missing a step

Calendar cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
#---- This uses the provided calendar for the output -----
dateFormat.setCalendar(cal); 
System.out.println(dateFormat.format(cal.getTime()));
查看更多
登录 后发表回答