How to get current timestamps in Java

How to get current timestamps in Java

Hi,

I want to get the current Timestamp in Java and then use it in JDBC program. How to get current timestamps in Java?

Thanks

View Answers

July 19, 2017 at 10:35 AM

Hi,

In Java using get the current Timestamp with the following code:

package net.roseindia;

import java.sql.Timestamp;
import java.util.Date;

/*
 * Example of getting current timestamp in Java 
 */
public class CurrentTimestamp {

    public static void main(String[] args) {
        //Using System current milliseconds
        Timestamp v1 = new Timestamp(System.currentTimeMillis());
        System.out.println(v1);

        //With the date object
        Date dt = new Date();
        Timestamp v2 = new Timestamp(dt.getTime());
        System.out.println(v2);

  }

}

First method is get the Timestamp using the:

System.currentTimeMillis()

function.

You can also also get it with the current Date.

    Date dt = new Date();
    Timestamp v2 = new Timestamp(dt.getTime());
    System.out.println(v2);

Thanks

Ads









Related Tutorials/Questions & Answers:
How to get current timestamps in Java
How to get current timestamps in Java  Hi, I want to get the current Timestamp in Java and then use it in JDBC program. How to get current timestamps in Java? Thanks   Hi, In Java using get the current Timestamp
How to get the current date in Java?
How to get the current date in Java?  Hi, I am new to Java and trying to write a program to get the current date. How to get the current date in Java? Thanks   Hi, Check the tutorial: Java program to get current date
Advertisements
Java current date - How to get current date in Java
In this section, you will learn about displaying current date and time in Java
How to get system current cursor type
How to get system current cursor type  In Windows how can i get the current mouse/keyboard cursor type like 'text cursor' from my java application
Get current working directory in Java
Get current working directory in Java In this section we will discuss about how to get the the current working directory in java. Current working... to get current directory in Java with Example public class
Java program to get current date
Java program to get current date now       In this example program we have to get the current date at present now. We have used Calendar class for getting the current
Java Example program to get the current working directory
Java Example program to get the current working directory... program illustrates to get the current working directory (CWD). With the File class object we can get two types of paths in java. These are as follows
JDBC: Get current Date and Time Example
JDBC: Get current Date and Time Example In this section, you will learn how to get current Date and current time using JDBC API. Get current Date and Time... in the format 'YYYY-MM-DD' You can get current date by writing 
Get current page URL from backing bean
Get current page URL from backing bean  How to get current page URL from backing bean
JDBC: Get current TimeStamp Example
JDBC: Get current TimeStamp Example In this tutorial, we are going to describe how to get current TIMESTAMP using JDBC API. Get current TimeStamp : CURRENT... object of Statement statement = con.createStatement(); // Get current
get current date in java in yyyy mm dd hh mm ss format
get current date in java in yyyy mm dd hh mm ss format  Hi, I want to get the current date into string yyyy mm dd hh mm ss format. How to get current date in java in yyyy mm dd hh mm ss format in a Java program? Thanks  
How to get Java SDK
How to get Java SDK  Hi, I have purchased a new computer and installed windows 7. Now I have to get the Java SDK and install on my computer for developing and testing Java programs. So, can anyone tell me How to get Java SDK
Get current Date and Time
Get current Date and Time In this section we will discuss about how to get current date and time in java. Java provide two classes, Date and calendar which will display the current date and time in java. SimpleDateFormat is used 
how to get this xml syntx in java??????
how to get this xml syntx in java??????  how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
how to get this xml syntx in java??????
how to get this xml syntx in java??????   how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
Java Get Month
and how to get the current date and time. But in this example, we are going to show the current month of the existing year. This Java get month example is simply... Java Get Month      
How to get enum value in Java
How to get enum value in Java  Hi all, i am looking for a solution to get an enum value from index in Java. Please suggest. Thanks
ModuleNotFoundError: No module named 'timestamps'
'timestamps' How to remove the ModuleNotFoundError: No module named 'timestamps' error? Thanks   Hi, In your python environment you...ModuleNotFoundError: No module named 'timestamps'  Hi, My Python
How to convert current date to dd mm yyyy format in Java?
How to convert current date to dd mm yyyy format in Java?  Hi, How to convert current date to dd mm yyyy format in Java? In my program I have to display the current date in the dd-mm-yyyy format to user on the website. How
How to get length of string in Java?
How to get length of string in Java?  What is the method of String for getting the length? How to get length of string in Java? Tell me code fast... Tutorials"; int length = str.length(); You have to use the str.length() to get
how to get browser cookies in java
how to get browser cookies in java  Hi, How to get cookies in serverlet and then print? how to get browser cookies in java? Thanks   Hi, Use following code: Cookie[] cookies = request.getCookies(); for (int i = 0
Getting the Current Working Directory in Java
C:\nisha>javac GetCurrentDir.java C:\nisha>java GetCurrentDir Current Working Directory : C:\nisha C:\nisha> Download this example
How to convert current date to mm dd yyyy format in Java?
How to convert current date to mm dd yyyy format in Java?  Hi, How to convert current date to mm dd yyyy format in Java? Share me the program... for converting current date into MM-dd-yyyy format. Here is full code: package
How to get day of week in Java?
How to get day of week in Java?  Hi, How to get day of week in Java? Thanks   Hi, Calendar class is very useful in Java. You can use this class for getting the day of the week. Here is sample code: int weekday
How to print current time in r?
How to print current time in r?  Hi, I am learning and want to print current date and time on console. How to print current time in R Programming... and time. Following is the example usage of above function to get current date
How to format current date into yyyy-mm-dd in Java
database.I am searching code formatting the current date into this format. I have to do this in Java Programming Language. How to format current date...How to format current date into yyyy-mm-dd in Java  Hi, I have
how to get file extension in java
how to get file extension in java  Hi, I want to get the file extension in Java. how to get file extension in java? Thanks   Hi, Here is sample code to get file extension in Java: package examples; import
In Java how to get size of class and object?
In Java how to get size of class and object?  In Java how to get size of class and object
In Java how to get size of class and object?
In Java how to get size of class and object?  In Java how to get size of class and object
How to get UTF-8 working in java webapps?
How to get UTF-8 working in java webapps?  How to get UTF-8 working in java webapps
How to display the current time
How to display the current time  I was develop online web portal. In this web portal i am going to display time. I am using <%= new java.util.Date() %> to display the time. But it display the time after that cannot changed

Ads