Java получить дату из timestamp

Class Timestamp

A thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds. A Timestamp also provides formatting and parsing operations to support the JDBC escape syntax for timestamp values.

  • 19 , which is the number of characters in yyyy-mm-dd hh:mm:ss
  • 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff. ] and s represents the scale of the given Timestamp, its fractional seconds precision.

Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component. The fractional seconds — the nanos — are separate. The Timestamp.equals(Object) method never returns true when passed an object that isn’t an instance of java.sql.Timestamp , because the nanos component of a date is unknown. As a result, the Timestamp.equals(Object) method is not symmetric with respect to the java.util.Date.equals(Object) method. Also, the hashCode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation.

Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date . The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.

Источник

How to convert Java TimeStamp to Date

In Java, TimeStamp can be converted into Date using the constructor of the Date class of the java.util package.

It must be noted that Date class takes an argument as a long value hence the TimeStamp object needs to be converted into long. This is done using the getTime() method of Timestamp class of the java.sql package.

Example 1:

Here, the TimeStamp is converted into Date by using the Date constructor. See the example below.

import java.sql.Timestamp; import java.util.Date; public class StudyTonight < public static void main(String args[]) < Timestamp t = new Timestamp(System.currentTimeMillis()); //for current time in milliseconds Date d = new Date(t.getTime()); System.out.println(d); >>

Thu Sep 24 05:28:11 UTC 2020

Example 2:

Also, the Timestamp class extends the Date class. Hence, the instance of the Timestamp class can directly be assigned into Date and the output like Timestamp can be obtained. But, it is undesirable because the milliseconds and nanoseconds can be lost in this case.

import java.sql.Timestamp; import java.util.Date; public class StudyTonight < public static void main(String args[]) < Timestamp t = new Timestamp(System.currentTimeMillis()); Date d = t; System.out.println(d); >>

Example 3:

Here, the TimeStamp is converted into Date with formatting into various formats.

import java.sql.Timestamp; import java.text.DateFormat; import java.util.Date; import java.text.SimpleDateFormat; public class StudyTonight < public static void main(String args[]) < Timestamp stamp = new Timestamp(System.currentTimeMillis()); Date date = new Date(stamp.getTime()); DateFormat f = new SimpleDateFormat("yyyy-MM-dd"); DateFormat f1 = new SimpleDateFormat("yyyy/MM/dd"); String d = f.format(date); String d1 = f1.format(date); System.out.println(d); System.out.println(d1); >> 

Источник

TimeStamp to Date in Java

TimeStamp to Date in Java

In Java timestamp is defined as a sequence for providing formatted or encoded data whenever any event occurs such as displaying date and time of day where this timestamp class can be converted to date class in java using Java.util library which needs to be imported when dealing with a timestamp in Java. In general, the timestamp is converted to date using the date class of Java.util class for displaying the SQL timestamp that contains both date and time value and there are several ways to convert timestamp to date where this timestamp can be obtained by using java.util.Date which belongs to java.util library.

Читайте также:  Список

Syntax of TimeStamp to Date in Java

There are 3 different ways to convert timestamp to date in java. Let us see the syntax of these different methods separately.

  • Using Constructor: One way for the conversion of the timestamp to date is done using the constructor of java.util.Date class. Let us see the syntax for this constructor:
  • Using Date Reference: Where there is timestamp class which extends the Date class where the instance of timestamp class is assigned to Date. Therefore the syntax for this description is given as follows:
Timestamp t = new Timestamp(); Date d = t;
  • Using Calendar Class: To obtain date from timestamp which is also present in calendar class will also provide date. Therefore this can be demonstrated in the syntax as:
Timestamp t = new Timestamp(); Calendar c = Calendar.getInstance(); Calendar.getTime();

How to Convert Timestamp to Date in Java?

In Java, the timestamp is defined as the time and the date format that is displayed using timestamp class which is done using java.util class for using date and calendar class whereas there is java.sql package for using timestamp class in java to implement timestamp in the java program. The conversion of the timestamp to date would result in displaying both date and time in milliseconds which is usually a human-readable format. Therefore exact working of the timestamp to date conversion is done using 3 different ways and is done using timestamp class using java.util or java.sql package.

1. Using Constructor

In Java, there is java.util.Date class which is provided by the java.util package and we will be using the constructor of this class to convert timestamp to date. The working of this method of conversion is firstly it will obtain the value which is long value as a parameter from the Date class constructor where it results in timestamp object which will convert long value using getTime() method

Example
import java.sql.Timestamp; import java.util.Date; public class TimestampToDate < public static void main(String args[]) < Timestamp t = new Timestamp(System.currentTimeMillis()); Date d = new Date(t.getTime()); System.out.println(" Demonstration of timestamp to date conversion using date constructor : "); System.out.println(" \n "); System.out.println( d ); >>

1

In the above example, we can see that we have first imported java.sql, Timestamp package, and java.util.Date package for using date constructor we need Date class and to use timestamp we use Timestamp class. Therefore, firstly we define and declare a variable “t” which belongs to the Timestamp class and we define it using “new” and we pass the currentTimeMills() function to display the timestamp which is later converted to date. Then we declare the Date variable of the Date class “d” which is defined using “new” where we are passing the getTime() function to convert the above timestamp into date format. Then using println we will display the converted date from the obtained timestamp. The output of the above program is as shown in the above screenshot.

Читайте также:  Data url attribute in html

2. Using Date References

This is another method of converting timestamp to date in Java using again the date class of java.util.date package. In this method, it just extends the date class where the timestamp instance is assigned to date directly and therefore the date object which results as output in the date format which was earlier in the timestamp. Let us see an example below for demonstrating such method in java:

Example
import java.sql.Timestamp; import java.util.Date; public class TimestampToDate < public static void main(String args[]) < Timestamp t = new Timestamp(System.currentTimeMillis()); Date d = t; System.out.println(" Demonstration of timestamp to date conversion using date reference : "); System.out.println(" \n "); System.out.println( d ); >>

2

In the above example, we can see it similar to the previous example but the only difference in the above program and this program is that instead of defining the date class here it will directly assign the timestamp instance “t” to date object “d”, here we do not use getTime() function as we are directly assigning the date object to timestamp instance and therefore here the output will look similar to timestamp but it is date format with time also including milliseconds. This can be seen in the above screenshot.

3. Using Calendar Class

Another method of converting timestamp to date in java is using calendar class which is rarely used by any developers. We can obtain calendar class by the java.util.Calendar package where we use this calendar class along with which we have to use getInstance() function to get timestamp instance and getTime() to get the date along with time which will be converted from the timestamp. Let us see an example below:

Example
import java.sql.Timestamp; import java.util.Date; import java.util.Calendar; public class TimestampToDate < public static void main(String args[]) < Timestamp t = new Timestamp(System.currentTimeMillis()); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(t.getTime()); System.out.println(" Demonstration of timestamp to date conversion using Calendar class : "); System.out.println(" \n "); System.out.println(calendar.getTime()); >>

3

In the above example, we can see we have imported Calendar class also from java.util.Calendar package and this example are similar to the first example where we used date class. In this example, we define the calendar instance by using the getInstance() function which will be in the timestamp format later by using getTime() the timestamp instance is converted to a date object which results as shown in the output in the above screenshot.

Conclusion

In this article, we conclude that the timestamp to date conversion in java is very simple. In Java, there is java.util package which provides different classes for supporting this conversion in various ways with different classes. It depends on the user on which method to use according to the user’s requirement and time accuracy which displays the result accurately as required.

This is a guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples. You may also have a look at the following articles to learn more –

89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

Читайте также:  What is session attribute in java

JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8

Источник

Java получить дату из timestamp

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.

Sets this Timestamp object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

Obtains an instance of Timestamp from a LocalDateTime object, with the same year, month, day of month, hours, minutes, seconds and nanos date-time value as the provided LocalDateTime .

Methods inherited from class java.util.Date

Methods inherited from class java.lang.Object

Constructor Detail

Timestamp

@Deprecated public Timestamp(int year, int month, int date, int hour, int minute, int second, int nano)

Timestamp

public Timestamp(long time)

Constructs a Timestamp object using a milliseconds time value. The integral seconds are stored in the underlying date value; the fractional seconds are stored in the nanos field of the Timestamp object.

Method Detail

setTime

public void setTime(long time)

Sets this Timestamp object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

getTime

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.

valueOf

toString

Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm:ss.fffffffff , where ffffffffff indicates nanoseconds.

getNanos

setNanos

public void setNanos(int n)

equals

equals

Tests to see if this Timestamp object is equal to the given object. This version of the method equals has been added to fix the incorrect signature of Timestamp.equals(Timestamp) and to preserve backward compatibility with existing class files. Note: This method is not symmetric with respect to the equals(Object) method in the base class.

before

after

compareTo

compareTo

hashCode

Returns a hash code value for this object. The result is the exclusive OR of the two halves of the primitive long value returned by the Date.getTime() method. That is, the hash code is the value of the expression:

 (int)(this.getTime()^(this.getTime() >>> 32)) 

The hashCode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation.

valueOf

public static Timestamp valueOf(LocalDateTime dateTime)

Obtains an instance of Timestamp from a LocalDateTime object, with the same year, month, day of month, hours, minutes, seconds and nanos date-time value as the provided LocalDateTime . The provided LocalDateTime is interpreted as the local date-time in the local time zone.

toLocalDateTime

Converts this Timestamp object to a LocalDateTime . The conversion creates a LocalDateTime that represents the same year, month, day of month, hours, minutes, seconds and nanos date-time value as this Timestamp in the local time zone.

from

Obtains an instance of Timestamp from an Instant object. Instant can store points on the time-line further in the future and further in the past than Date . In this scenario, this method will throw an exception.

toInstant

Converts this Timestamp object to an Instant . The conversion creates an Instant that represents the same point on the time-line as this Timestamp .

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Оцените статью