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

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

问题描述

我有一个 String 表示一个日期,我需要创建一个日期日历对象。我看过日期日历 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?

推荐答案

b

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = formatter.parse("01/29/02");

请参阅 SimpleDateFormat javadoc

See SimpleDateFormat javadoc for more.

将它变成日历,执行:

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

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

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