【问题标题】:get current date time in yyyy-MM-dd hh.mm.ss format以 yyyy-MM-dd hh.mm.ss 格式获取当前日期时间
【发布时间】:2013-12-17 04:12:04
【问题描述】:

我有一个应用程序,它总是只在一个时区运行,所以我不需要担心时区之间的转换。但是,日期时间必须始终以以下格式打印出来:

yyyy-MM-dd hh.mm.ss 

下面的代码无法打印正确的格式:

public void setCreated(){
    DateTime now = new org.joda.time.DateTime();
    String pattern = "yyyy-MM-dd hh.mm.ss";
    created  = DateTime.parse(now.toString(), DateTimeFormat.forPattern(pattern));
    System.out.println("''''''''''''''''''''''''''' created is: "+created);
}  

setCreated() 方法产生以下输出:

"2013-12-16T20:06:18.672-08:00"

如何更改 setCreated() 中的代码,以便打印出以下内容:

"2013-12-16 20:06:18"

【问题讨论】:

  • 你不想String pattern = "yyyy-MM-dd hh:mm:ss";吗?

标签: java


【解决方案1】:

你没有解析任何东西,你正在格式化它。您需要使用DateTimeFormatter#print(ReadableInstant)

DateTime now = new org.joda.time.DateTime();
String pattern = "yyyy-MM-dd hh.mm.ss";
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
String formatted = formatter.print(now);
System.out.println(formatted);

打印出来的

2013-12-16 11.13.24

这与您的格式不匹配,但我是根据您的代码,而不是您的预期输出。

【讨论】:

  • 谢谢。 +1。但是您的代码输出一个字符串。你能告诉我如何让它输出一个日期时间对象吗?
  • @CodeMed 哦,那我相信你有误会。 DateTimeDate 或任何时间对象都没有格式。输出时给它一个文本格式。一个DateTimerepresents an exact point on the time-line, but limited to the precision of milliseconds.
  • @CodeMed 在其toString() 返回值中有一个默认格式,但这又只是一个文本表示。日期瞬间本身就是自 linux epoch 以来的秒数/毫秒数。
  • 谢谢。我只想要一种简单的方法将这种格式泵入 MySQL TimeStamp 字段。我被应用程序的hibernate和jpa困住了。我们在这里的答案中有正确的格式。当必须有一些简单的方法将您的正确语法强制输入数据库时​​,我不想花费数小时探索复杂的代码。你有一个简单的建议吗?查看其他帖子:stackoverflow.com/questions/20625105/…
  • @CodeMed 您在控制台上玩游戏时看到的 MySQL 日期格式再次只是它实际存储内容的文本表示,它只是一个 bigint/long 字段 (YMMV)。在 JDBC 级别(与数据库的最低级别接口),Hibernate 可能使用setDate()。您需要找到 joda-hibernate 集成,它将您的 DateTime 对象在内部转换为 Date 对象,即。这将对您隐藏。
【解决方案2】:

现在在 Java 9 中,你可以使用这个:

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh.mm.ss"));

【讨论】:

    【解决方案3】:
     public static void main(String args[])
    {
    
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
    Date now = new Date();
    String strDate = sdfDate.format(now);
        System.out.println(strDate);
    }
    

    输出 2013-12-17 09:48:11

    【讨论】:

      【解决方案4】:

      试试这个

              SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
              Date now = new Date();
              String strDate = sdfDate.format(now);
              System.out.println(strDate);
      

      demo

      【讨论】:

        【解决方案5】:

        试试这个:

        org.joda.time.DateTime now = new org.joda.time.DateTime();
        String pattern = "yyyy-MM-dd hh.mm.ss";
        DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
        String formatted = formatter.print(now);
        LocalDateTime date = formatter.parseLocalDateTime(formatted);
        System.out.println(date.toDateTime());
        

        【讨论】:

        • 感谢您和 +1 抽出宝贵时间为这个老问题提供额外答案。
        猜你喜欢
        • 2011-12-06
        • 2013-10-04
        • 1970-01-01
        • 1970-01-01
        • 2011-09-09
        • 2011-06-29
        • 2014-07-09
        • 2016-01-02
        • 1970-01-01
        相关资源
        最近更新 更多