Java:在java.util.Date格式中返回一个字符串 [英] Java : Return a String in the java.util.Date Format

查看:89
本文介绍了Java:在java.util.Date格式中返回一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以这种格式201201收录输入,即YYYYMM格式。

I will be reciving the Input in this format 201201 , which is YYYYMM format .

现在我想要返回值201201,但它应该使用java.util.Date格式

Now i want to return the value 201201 as it is , but it it should be in a java.util.Date format

我很困惑

String strDate = "201201";  
SimpleDateFormat sdFormat = new SimpleDateFormat("YYYYMM"); 

现在我无法使用值返回java.util.Date格式as 201201

我已经编辑了YYYYMM格式的问题。

I ahve edited the question it must be in YYYYMM format .

我尝试过这种方式

public class StringToDate {
    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("yyyymm");

        try {
            Date today = df.parse("201201");
            System.out.println(df.format(today));


            //System.out.println("Today = " + df.format(today));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}


推荐答案

编辑:mm是分钟MM是月份

mm is minute MM is month

尝试:DateFormat df = new SimpleDateFormat(yyyyMM);

Try : DateFormat df = new SimpleDateFormat("yyyyMM");

然后使用parse方法:

Then use the parse method:

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat .html#parse%28java.lang.String%29

这篇关于Java:在java.util.Date格式中返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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