如何将java字符串转换为Date对象 [英] how to convert java string to Date object

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

问题描述

我有一个字符串

String startDate = "06/27/2007";

现在我必须获取Date对象。我的DateObject应该和startDate相同。

now i have to get Date object. My DateObject should be the same value as of startDate.

我这样做这样

DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);

但输出格式为


Jan 27 00:06:00 PST 2007.

Jan 27 00:06:00 PST 2007.


推荐答案

您基本上有效地将日期以字符串格式转换为日期对象。如果您打印出来,您将获得标准的日期格式输出。为了格式化之后,您需要将其转换为具有指定(以前指定的)格式的日期对象:

You basically effectively converted your date in a string format to a date object. If you print it out at that point, you will get the standard date formatting output. In order to format it after that, you then need to convert it back to a date object with a specified (already specified previously) format:

String startDateString = "06/27/2007";
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
    Date startDate;
    try {
        startDate = df.parse(startDateString);
        String newDateString = df.format(startDate);
        System.out.println(newDateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }

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

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