在Android中将字符串转换为日期格式 [英] Convert string to date format in android

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

问题描述

我正在尝试将字符串转换为日期格式。我尝试了许多方法来实现此目的,但是没有成功。我的字符串是 2012年1月17日。我想将其转换为 2011-10-17。
有人可以告诉我这样做的方法吗?如果您通过示例进行了工作,那将是真正的帮助!

I'm trying to convert string to date format.I trying lot of ways to do that.But not successful. my string is "Jan 17, 2012". I want to convert this as " 2011-10-17". Could someone please tell me the way to do this? If you have any worked through examples, that would be a real help!

推荐答案

public class Utils {

    public static void main(String[] args) {

        String mytime="Jan 17, 2012";
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MMM dd, yyyy");

        Date myDate = null;
        try {
            myDate = dateFormat.parse(mytime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
        String finalDate = timeFormat.format(myDate);

        System.out.println(finalDate);
    }
}

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

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