简体   繁体   English

如何将日期格式从yyyy-MM-dd更改为dd-MMM-yyyy

[英]How to change date format from yyyy-MM-dd to dd-MMM-yyyy

I am retrieving date from database. 我正在从数据库中检索日期。 Datatype is'date' with date format 2015-12-16. 数据类型为“日期”,日期格式为2015-12-16。

I need to set that date to my bean class variable.Datatype is Date with format 16-Dec-2015 我需要将该日期设置为我的bean类变量。数据类型是日期,格式为16-Dec-2015

These are the date formats i am using 这些是我正在使用的日期格式

      SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
              SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");

              Date date = null;
              String formatteddate = null;

                try {
                    formatteddate = formatter.format(rs.getDate("dol"));
                    System.out.println("formatteddate=============="+formatteddate);
                    date = formatter.parse(formatteddate);
                    System.out.println("date========="+date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }


              joborderbean.setDol(date);

formatteddate==============15-Dec-2015 formatteddate ============== 1-5-2015年12月

I want to display the above format ie,15-Dec-2015. 我想显示上述格式,即2015年12月15日。 But it is diplaying the below format 但它正在显示以下格式

date=========Tue Dec 15 00:00:00 IST 2015 日期==========星期二IST 2015年12月15日00:00:00

Please help me 请帮我

Do like following: 请执行以下操作:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateformat=fromatter.format(mysqldate);
System.out.println("first date format"+ dateformat);
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
String dateformat1=formatter1.format(mysqldate);
System.out.println("second date format"+dateforamt1);

also you can edit the sql fired to get the result like 您也可以编辑触发的sql以得到如下结果

 select DATE_FORMAT(date_field,'%Y-%b-%d')

and

select DATE_FORMAT(date_field,'%d-%b-%Y')

First of all the Resultset objects getDate method return a java.sql.Date object. 首先, Resultset对象的getDate方法返回一个java.sql.Date对象。 So in order to store the Date in the date object use : 因此,为了将Date存储在date对象中,请使用:

date = rs.getDate("dol");

instead of the code: 而不是代码:

formatteddate = formatter.format(rs.getDate("dol"));
System.out.println("formatteddate=============="+formatteddate);
date = formatter.parse(formatteddate);
System.out.println("date========="+date);

now in order to test the date you can use: 现在,为了测试日期,您可以使用:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
System.out.printLn("date========="+formatter.format(date));

This will give you the required output. 这将为您提供所需的输出。

The last output you are getting because of the code System.out.println("date========="+date); 由于代码System.out.println("date========="+date); Here the toString method of the date object is getting called which has a default as in the documentation as : 在这里,将调用date对象的toString方法,该方法在文档中的默认值为:

Converts this Date object to a String of the form: 将此Date对象转换为以下形式的字符串:

 dow mon dd hh:mm:ss zzz yyyy 

where: 哪里:

  • dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat). 陶氏是星期几(星期日,星期一,星期二,星期三,星期四,星期五,星期六)。
  • mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec). 星期一是月份(1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月)。
  • dd is the day of the month (01 through 31), as two decimal digits. dd是月份中的日期(01到31),为两位十进制数字。
  • hh is the hour of the day (00 through 23), as two decimal digits. hh是一天中的小时(00到23),用两位十进制数字表示。
  • mm is the minute within the hour (00 through 59), as two decimal digits. mm是小时(00到59)之间的分钟,以两位十进制数字表示。
  • ss is the second within the minute (00 through 61, as two decimal digits. ss是分钟内的秒数(00到61,为两位十进制数字)。
  • zzz is the time zone (and may reflect daylight saving time). zzz是时区(可能反映夏令时)。 Standard time zone abbreviations include those recognized by the method parse. 标准时区缩写包括方法解析识别的缩写。 If time zone information is not available, then zzz is empty - that is, it consists of no characters at all. 如果没有时区信息,则zzz为空-也就是说,它根本不包含任何字符。
  • yyyy is the year, as four decimal digits. yyyy是年份,以四个十进制数字表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM