如何将日期字符串转换为日期或日历对象? [英] How to convert a date String to a Date or Calendar object?

查看:49
本文介绍了如何将日期字符串转换为日期或日历对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期的 String 表示,我需要从中创建一个 DateCalendar 对象.我已经查看了 DateCalendar API,但除了创建我自己的丑陋解析方法之外,还没有找到任何可以做到这一点的东西.我知道一定有办法,有人知道解决方案吗?

I have a String representation of a date that I need to create a Date or Calendar object from. I've looked through Date and Calendar APIs but haven't found anything that can do this other than creating my own ugly parse method. I know there must be a way, does anyone know of a solution?

推荐答案

简述:

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
try {
  Date date = formatter.parse("01/29/02");
} catch (ParseException e) {
  e.printStackTrace();
}

参见 SimpleDateFormat javadoc 了解更多.

See SimpleDateFormat javadoc for more.

并将其转换为Calendar,请执行以下操作:

And to turn it into a Calendar, do:

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

这篇关于如何将日期字符串转换为日期或日历对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆