日期格式JAVA [英] Date Format JAVA

查看:103
本文介绍了日期格式JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想格式化 2012-05-04 00:00:00.0 04-May-2012 。我已经尝试了以下步骤。

I want to format 2012-05-04 00:00:00.0 to 04-MAY-2012. i have tried it with below steps.

    SimpleDateFormat sdf = new SimpleDateFormat(
            "yyyy-MM-dd 'T' HH:mm:ss.SSS");

    Date date;
    String dateformat = "";
    try {
        date = sdf.parse("2012-05-04 00:00:00.0");
        sdf.applyPattern("DD-MON-RR");
        dateformat = sdf.format(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但是我得到以下异常。

but i got below exception.

java.text.ParseException: Unparseable date: "2012-05-04 00:00:00.0"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at com.am.test.Commit.main(Example.java:33)`

我该怎么做?

推荐答案


  1. 删除第一个模式中的额外'T'

  2. 第二种格式不正确,它应该是dd-MMM-yyyy。

查看 SimpleDateFormat

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class temp2 {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        Date date;
        String dateformat = "";
        try {
            date = sdf.parse("2012-05-04 00:00:00.0");
            sdf.applyPattern("dd-MMM-yyyy");
            dateformat = sdf.format(date);
            System.err.println(dateformat);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

这篇关于日期格式JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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