简体   繁体   English

在Java上将日期格式转换为其他格式

[英]Converting Date format to other format on Java

I have been developing application with GWT on Java. 我一直在用Java上的GWT开发应用程序。 I used calendar tool for my project on this site . 我在此站点上为我的项目使用了日历工具。 When i select any date, give this format (23-Aug-13) to me. 当我选择任何日期时,请将此格式(13-Aug-13)给我。

I use this code: 我使用以下代码:

final DateField txtBirthofDay = new DateField("Birth of Day", "dob", 190);
txtBirthofDay.setValue(new Date());

I want to insert this date to database. 我想将此日期插入数据库。 So, I have to convert this format (2013-08-23). 因此,我必须转换此格式(2013-08-23)。 I converted lots of date format each other before but I didn't convert string type (Aug). 我之前互相转换了许多日期格式,但没有转换字符串类型(八月)。

Reference these formats Java Date Format Docs : 引用以下格式的Java日期格式文档

日期时间格式

try this: 尝试这个:

SimpleDateFormat dt = new SimpleDateFormat("dd-MMM-yyyy");
Date date = dt.parse("23-Aug-2013");;

// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dt1.format(date));

you should go for SimpleDateformat as per your requirement. 您应该根据需要选择SimpleDateformat。 Click this link for how to use simpledateformat in java 单击此链接以了解如何在Java中使用simpledateformat

link 链接

Try this, 尝试这个,

    DateFormat df=new SimpleDateFormat("dd-MMM-yy");
    Date date=df.parse("23-Aug-13");
    System.out.println(df.format(date));
    df=new SimpleDateFormat("yyyy-MM-dd");
    String requiredFormatedDate=df.format(date);
    System.out.println(requiredFormatedDate);// 2013-08-23

SimpleDateFormat formats your Date to the given pattern SimpleDateFormat将日期格式化为给定的模式

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(sdf.format(new Date()));

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

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