Java解析字符串到目前为止 [英] Java parse string to date

查看:137
本文介绍了Java解析字符串到目前为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析Java中的字符串到日期对象。

我的字符串是: String date = 2013-04-13 21:00:00;
代码是:

  String myFormatString =yyyy-mm-dd hh:mm: ss 
Date date1 = new SimpleDateFormat(myFormatString,Locale.ENGLISH).parse(date);
System.out.println(date1);

我期望输出为: Sun Apr 13 21:00 :00 GMT + 00:00 2013

但是我得到的是: Sun Jan 13 21:00:00 GMT + 00:00 2013



你能看到为什么吗?

解决方案

月份以M表示,m表示分钟。

  String myFormatString =yyyy-MM-dd hh: mm:ss; 

详细信息位于此页面


I'm trying to parse a string to date object in Java.
My string is: String date = 2013-04-13 21:00:00; The code is:

    String myFormatString = "yyyy-mm-dd hh:mm:ss";
    Date date1 = new SimpleDateFormat(myFormatString, Locale.ENGLISH).parse(date);
    System.out.println(date1);

I'm expecting the output to be: Sun Apr 13 21:00:00 GMT+00:00 2013
but what I get is: Sun Jan 13 21:00:00 GMT+00:00 2013

Can you see why?

解决方案

Months are represented by "M", "m" is for minutes.

String myFormatString = "yyyy-MM-dd hh:mm:ss";

Detailed information is on this page.

这篇关于Java解析字符串到目前为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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