Saturday, November 23, 2019

How To Popular Off Electrical Current Timestamp Value Inwards Java

Do you lot know that Java developer Google for fifty-fifty simplest of things? Yes, that's truthful together with I tin ship away country it because fifty-fifty I produce that :-). The electrical flow timestamp value is i of them. In UNIX you lot tin ship away only piece of job the appointment ascendancy to acquire the electrical flow appointment together with fourth dimension but how produce you lot acquire that inwards your Java program? Well, you lot tin ship away acquire the electrical flow timestamp inwards Java past times using Date together with Timestamp course of pedagogy of JDK. Note, in that place is "s" instead of "S" inwards Timestamp. Since Date inwards Java contains both appointment together with time, it tin ship away endure used every bit Timestamp value every bit good but when you lot impress Date, it shows fourth dimension inwards the local timezone. If you lot postulate just the timestamp, hence you lot postulate to convert the java.util.Date to java.sql.Timestamp. You tin ship away convert a Date to Timestamp past times using the getTime() method which returns the release of millisecond from the Epoch.

You should likewise recollect that Timestamp is a subclass of java.util.Date but doesn't accolade Liskov exchange principle, which agency you lot cannot move past times a Timestamp when Date is expected. Why? because it likewise includes additional nanosecond value which is fitted in that place to confirm database DATETIME value, which supports nanosecond precision. You tin ship away likewise read my post why Timestamp cannot endure used inwards house of Date inwards Java for to a greater extent than details.

Btw, In Java 8, you lot tin ship away likewise piece of job the LocalDateTime class to acquire the electrical flow timestamp inwards Java. In this article, I'll exhibit you lot a pair of ways to acquire the electrical flow timestamp value. If you lot are interested to acquire to a greater extent than near novel features of Java SE 8 including Date together with Time API, lambda expressions, streams, together with other small-scale enhancement e.g. joining String using String.join() methods, delight see Java SE 8 for Really Impatient By Cay S. Horstmann.




Current Timestamp inwards Java

Here is a sample code snippet you lot tin ship away piece of job to retrieve the electrical flow timestamp value using java.util.Date course of pedagogy together with java.sql.Timestamp inwards Java. Remember the java.util.Date course of pedagogy is named Date, but. it contains both appointment together with fourth dimension value, dissimilar LocalDate course of pedagogy of Java 8, which entirely contains Date information without whatsoever fourth dimension details. There is a divide course of pedagogy LocalTime for representing fourth dimension without appointment together with LocalDateTime to correspond appointment amongst time.

On the other hand, java.sql.Timestamp is a subclass of Date but has additional nanosecond value for to a greater extent than precision together with endure compatible amongst DATETIME type of many relational database e.g. SQL Server, MySQL, together with Oracle.

Anyway, hither is our code prior to Java 8 world:

Date right away = new java.util.Date(); Timestamp electrical flow = new java.sql.Timestamp(now.getTime()); System.out.println("current timestamp: " + current); System.out.println("current date: " + now);  Output electrical flow timestamp: 2016-06-16 17:33:14.53 electrical flow date: Thu Jun sixteen 17:33:14 SGT 2016

If you lot run this program, it volition impress the electrical flow appointment together with fourth dimension value, only elbow grease that earlier you lot read further.

 Do you lot know that Java developer Google for fifty-fifty simplest of things How to acquire electrical flow TimeStamp value inwards Java


Btw, if you lot desire your electrical flow timestamp into a different String format e.g. "yyyy.MM.dd.HH.mm.ss" hence you lot postulate to piece of job the SimpleDateFormat course of pedagogy to format the timestamp every bit shown below:

String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(current); System.out.println("current timestamp every bit yyyy.MM.dd.HH.mm.ss " + timeStamp);  Output electrical flow timestamp every bit yyyy.MM.dd.HH.mm.ss 2016.06.16.17.41.19

You tin ship away format the appointment to whatsoever way you lot want, only endure aware of the syntax used for formatting dates inwards Java every bit shown here.  Btw, these are non the entirely way to acquire the electrical flow timestamp value, for example, if you lot only postulate long milliseconds from the 1st Jan 1970 hence you lot tin ship away piece of job the System.currentTimeMillis() which provide millisecond from Epoch and System.nanoTime() which provide the electrical flow timestamp value inwards nanoseconds.

 You tin ship away likewise read a adept Java mass e.g. Core Java for Impatient past times Cay S. Horstmann to acquire to a greater extent than near fundamentals of Java e.g. date, time, together with calendar course of pedagogy inwards Java. It fifty-fifty covers Java SE 8 novel Date together with Time API, which is only a bonus.

 Do you lot know that Java developer Google for fifty-fifty simplest of things How to acquire electrical flow TimeStamp value inwards Java



Java Program to acquire electrical flow Timestamp value

hither is our sample Java plan to acquire the electrical flow fourth dimension postage stamp value. You tin ship away run it, tweak it to sympathise what nosotros are doing. It's simple, nosotros are only converting a appointment to the timestamp to acquire the electrical flow timestamp value. Later nosotros are formatting into the required format using the SimpleDateFormat class, i of the utility course of pedagogy but endure careful it's non thread-safe, run into here to acquire to a greater extent than near using SimpleDateFormat course of pedagogy inwards Java.

package test;  import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date;  /**  * Java Program to acquire electrical flow Timestamp value  */ public class TimestampTest {    public static void main(String[] args) {      Date right away = new java.util.Date();     Timestamp electrical flow = new java.sql.Timestamp(now.getTime());     System.out.println("current timestamp: " + current);     System.out.println("current date: " + now);      // printing electrical flow timestamp every bit "yyyy.MM.dd.HH.mm.ss"     String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss")         .format(current);     System.out.println("current timestamp every bit yyyy.MM.dd.HH.mm.ss " + timeStamp);    }  }  Output electrical flow timestamp: 2016-06-16 17:41:19.305 electrical flow date: Thu Jun sixteen 17:41:19 SGT 2016 electrical flow timestamp every bit yyyy.MM.dd.HH.mm.ss 2016.06.16.17.41.19


That's all near how to acquire the electrical flow timestamp value inwards Java. Just remember, that Timestamp is a course of pedagogy inwards the java.sql bundle together with its refer include "s" non "S". If you lot postulate a long timestamp value hence you lot tin ship away likewise convert a java.util.Date to Timestamp past times using getTime() method which provide a release of millisecond from Epoch time. Other alternatives include System.currentTimeMillis() which provide millisecond from Epoch and System.nanoTime() which provide the electrical flow timestamp value inwards nanoseconds.


Other Date together with Time tutorials for Java developers
  • How to convert java.sql.Date to java.util.Date inwards Java? (example)
  • How to format Date inwards Java using SimpleDateFormat? (example)
  • How to display Date inwards multiple timezones inwards Java? (example)
  • How to convert java.util.Date to java.sql.Timestamp inwards JDBC? (example)
  • How to convert String to Date inwards Java? (answer)
  • Difference betwixt java.util.Date together with java.sql.Timestamp inwards JDBC? (answer)
  • How to convert java.util.Date to java.sql.Date inwards JDBC? (answer)
  • How to parse String to Date inwards Java using JodaTime library? (example)


Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!


No comments:

Post a Comment