Date Formating:I would like to obtain the following date format 25/11/1990 14:35 based on a Timestamp of format '0001-01-01-00.00.00.000000'

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

Date Formating:I would like to obtain the following date format 25/11/1990 14:35 based on a Timestamp of format '0001-01-01-00.00.00.000000'

问题

我想要获取以下日期格式 25/11/1990 14:35,基于格式为'0001-01-01-00.00.00.000000'的时间戳。

需要通过 Angular 6 或 Java 8 来实现。

请提供任何相关的解决方案。

我的方法:

Timestamp timestamp = Timestamp.valueOf("1990-11-25 01:02:03.123456789");//来自数据库的值。
String str = timestamp.toString();
str.substring();

这对于向用户显示有帮助,但由于我将其转换为字符串,我无法将其存储在数据库中,因为数据库只会存储时间戳格式。

英文:

I would like to obtain the following date format 25/11/1990 14:35 based on a Timestamp of format '0001-01-01-00.00.00.000000'.

It needs to be done either via Angular 6 or Java 8.

Please provide any relevant solutions.

My Approach:

Timestamp timestamp = Timestamp.valueOf("1990-11-25 01:02:03.123456789")//Value from Db.
String str = timestamp.tostring();
str.substring();

This is helping me for displaying to the user but as I am converting it to a string I am unable to store it in DB Since DB will only store Timestamp format.

答案1

得分: 0

java.time和JDBC 4.2

我强烈推荐您使用java.time,这是现代的Java日期和时间API,而不是旧的Timestamp类。以下示例代码片段展示了如何从数据库检索时间戳并为用户进行格式化:

DateTimeFormatter formatter = DateTimeFormatter
        .ofLocalizedDateTime(FormatStyle.SHORT)
        .withLocale(Locale.forLanguageTag("pt"));

PreparedStatement query = yourDatabaseConnection.prepareStatement(
        "select your_timestamp_column from your_table where id = 4;");
ResultSet rs = query.executeQuery();
if (rs.next()) {
    OffsetDateTime dateTime = rs.getObject("your_timestamp_column", OffsetDateTime.class);
    String str = dateTime.format(formatter);
    System.out.println(str);
}

示例输出可能是

> 25/11/1990 14:35

请在我放置pt的位置插入您的用户的语言标签(例如葡萄牙语)。或者省略withLocale调用,以依赖于您的JVM的默认区域设置。请注意使用getObject()而不是getTimestamp()

在代码中,我使用了OffsetDateTime,基于您的数据库中的数据类型是建议用于时间戳的timestamp with time zone。在这种情况下,您可能还希望在格式化之前将时间戳转换为用户的时区。

如果数据类型是没有时区的timestamp,则可以完全相同的方式使用LocalDateTime

链接: Oracle教程:日期时间,解释了如何使用java.time。

英文:

java.time and JDBC 4.2

I warmly recommend that you use java.time, the modern Java date and time API, for your date and time work, not the old Timestamp class. This example snippet includes how you retrieve the timestamp from your database and format it for the user:

	DateTimeFormatter formatter = DateTimeFormatter
			.ofLocalizedDateTime(FormatStyle.SHORT)
			.withLocale(Locale.forLanguageTag("pt"));
	
	PreparedStatement query = yourDatabaseConnection.prepareStatement(
			"select your_timestamp_column from your_table where id = 4;");
	ResultSet rs = query.executeQuery();
	if (rs.next()) {
		OffsetDateTime dateTime
				= rs.getObject("your_timestamp_column", OffsetDateTime.class);
		String str = dateTime.format(formatter);
		System.out.println(str);
	}

Example output would be

> 25/11/1990 14:35

Please insert the language tag of your user where I put pt (for Portuguese). Or leave out the withLocale call to rely on the default locale of your JVM. And note the use of getObject() rather than getTimestamp().

I have used OffsetDateTime in the code based on the assumption that the datatype in your database is timestamp with time zone, which is what is recommended for timestamps. In this case you may also want to convert the timestamp to the user’s time zone before formatting it.

If the datatype is timestamp without time zone, instead use LocalDateTime in exactly the same manner.

Link: Oracle tutorial: Date Time explaining how to use java.time.

huangapple
  • 本文由 发表于 2020年4月6日 20:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61059864.html
匿名

发表评论

匿名网友

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

确定