简体   繁体   English

如何使用GWT将日期时间字符串转换为日期?

[英]How to convert String of datetime to date using GWT?

In mysql, i have a field time_entered of type datetime (sample data: 2012-06-20 16:00:47). 在mysql中,我有一个time_entered类型为datetime(示例数据:2012-06-20 16:00:47)。 I also have a method, getTimeEntered(), that returns the value as String. 我还有一个方法getTimeEntered(),它返回值为String。 I want to display the date in this format 2012-06-20 using DateTimeFormat from GWT. 我想使用GWT的DateTimeFormat以2012-06-20格式显示日期。

here's my code: 这是我的代码:

String date = aprHeaderDW.getTimeEntered();
DateTimeFormat fmt = DateTimeFormat.getFormat("MM-dd-yyyy");
dateEntered.setText("" + fmt.format(date));

The problem is, the format method doesn't accept arguments as String. 问题是,format方法不接受String参数。 So if there's only a way I could convert the date from String to Date type, it could probably work. 因此,如果只有一种方法可以将日期从String转换为Date类型,那么它可能会起作用。 I tried typecasting but didn't work. 我尝试了类型转换,但没有工作。

You should be able to just use DateTimeFormat . 您应该只能使用DateTimeFormat

Date date = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
String dateString = DateTimeFormat.getFormat("yyyy-MM-dd").format(date);

Otherwise there is a light-weight version of SimpleDateFormat that supports this pattern. 否则,有一个轻量级的SimpleDateFormat支持这种模式。

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");

Hi There are two options. 您好有两种选择。

The first is as it is already a string you could use a regular expression to modify the format. 第一个是因为它已经是一个字符串,你可以使用正则表达式来修改格式。

The second is using a SimpleDateFormater you can parse the string to a date then back again. 第二种是使用SimpleDateFormater,您可以将字符串解析为日期然后再返回。 For example: 例如:

public class DateMerge {

    public static void main(String arg[]) 
    {
        String out = dateConvert("2012-06-20 16:00:47");
        System.out.println(out);
    }

    public static String dateConvert (String inDate)
    {
        try {
         DateFormat formatter ; 
         Date date ; 
          formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          date = (Date)formatter.parse(inDate);
          formatter = new SimpleDateFormat("dd-MM-yyyy");
          String outDate = formatter.format(date);
          return outDate;

          } catch (ParseException e)
          {System.out.println("Exception :"+e);  }  


    return null;
    }
}

You may use like this. 你可以这样使用。

    String date = "2012-06-20 16:00:47";

    SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
    String lDate=sf.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date));

    System.out.println(lDate);

Output: 输出:

2012-06-20

After trying a lot of times I came up with a solution, based on @Keppil and adding my own code. 经过多次尝试,我想出了一个基于@Keppil并添加自己代码的解决方案。

Here's Keppil's suggested solution for converting String datetime into Date type: 这是Keppil建议的将String datetime转换为Date类型的解决方案:

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");

...but my second requirement is to display just the date like 2012-06-20. ...但我的第二个要求是只显示2012-06-20这样的日期。 Even though I removed HH:mm:ss, it still displayed the time like this 2012-06-20 00:00:00. 即使我删除了HH:mm:ss,它仍然显示时间,如2012-06-20 00:00:00。

Here's my final solution: 这是我的最终解决方案:

Date date = null;
String d = rs.getString(SQL_CREATION_TIME); // assigns datetime value from mysql

// parse String datetime to Date
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(d);
System.out.println("time entered: "+  date);
} catch (ParseException e) { e.printStackTrace(); }

// format the Date object then assigns to String
Format formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd");
String s = formatter.format(date);

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

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