Java如何获取日期格式:dd/MM/yyyy HH:mm:ss.SS(带有Date变量)

huangapple go评论81阅读模式
英文:

Java How to get date format : dd/MM/yyyy HH:mm:ss.SS” with Date variable

问题

我想要更改我的日期变量的格式:

private java.util.Date utilDate = new java.util.Date();

我将其用于在我的数据库(postgresql)中设置日期:

stmt.setDate(2, new java.sql.Date(utilDate.getTime()));

我该如何将我的当前格式:dd/MM/yyyy 更改为 dd/MM/yyyy HH:mm:ss.SS ?

英文:

I wouldlike to change format of my date variable :

private java.util.Date utilDate = new java.util.Date();

I use it to set Date in my database (postgresql) :

stmt.setDate(2, new java.sql.Date(utilDate.getTime()));

How can I transform my actual format : dd/MM/yyyy to dd/MM/yyyy HH:mm:ss.SS ?

答案1

得分: 1

    public static void main(String[] args) {
        Date utilDate = new Date();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS", Locale.US);
        System.out.println(dateFormat.format(utilDate.getTime()));
    }

输出:

12/10/2020 19:45:50.552

英文:
    public static void main(String[] args) {
        Date utilDate = new Date();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS", Locale.US);
        System.out.println(dateFormat.format(utilDate.getTime()));
    }
    

Output:

> 12/10/2020 19:45:50.552

答案2

得分: 0

以下是翻译好的内容:

你想设置的数据库列的类型是什么?Date 还是 Timestamp

你可以在这里找到 POSTGRESQL 支持的所有 DateTime 数据类型。

关于 Java 对于 DateTime 的实现(或者说缺乏实现),可以参考这里

示例:如何在 Java 中获取当前的日期和时间:

import java.time.LocalDateTime; // 导入 LocalDateTime 类
import java.time.format.DateTimeFormatter; // 导入 DateTimeFormatter 类

public class MyClass {
  public static void main(String[] args) {
    LocalDateTime myDateObj = LocalDateTime.now();
    System.out.println("格式化前:" + myDateObj);
    DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SS");

    String formattedDate = myDateObj.format(myFormatObj);
    System.out.println("格式化后:" + formattedDate);
  }
}

输出:

> 格式化前:2020-10-12T13:43:05.553697
> 格式化后:12/10/2020 13:43:05.553
英文:

What is the type of the column in your database that you are trying to set? Date or Timestamp?

You can find all datatypes supported by POSTGRESQL for Date and Time here

And for java's implementation (or lack there of) of DateTime refer to this

Example of getting current Date and Time in java:

import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class

public class MyClass {
  public static void main(String[] args) {
    LocalDateTime myDateObj = LocalDateTime.now();
    System.out.println("Before formatting: " + myDateObj);
    DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SS");

    String formattedDate = myDateObj.format(myFormatObj);
    System.out.println("After formatting: " + formattedDate);
  }
}

Output:

> Before Formatting: 2020-10-12T13:43:05.553697
> After Formatting: 12/10/2020 13:43:05.553

答案3

得分: 0

如果您想存储小时、分钟和秒数,您需要使用DateTime或LocalDateTime类型,而不是java.util.Date。Date只能存储年、月和日。然后,您可以使用一个格式化器。例如:

public DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SS");
英文:

If you want to store the hours, minutes and seconds, you have to use DateTime or LocalDateTime type instead of java.util.Date. Date only stores the year, month and day. <br>
Then, you can use a formatter. For example:

public DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;dd/MM/yyyy HH:mm:ss.SS&quot;);

huangapple
  • 本文由 发表于 2020年10月12日 19:25:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64316935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定