Getting current time in Java for inserting to MySQL

By: Emiley J. Emailed: 1766 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Quite often there is a need to get current time in Java in a specific format. I faced this issue when I had to insert the current time into MySQL. I solved it using a SimpleDateFormat class and a helper function. Here is the code.

The java.text.SimpleDateFormat class is very useful in formatting the date in Java. In my case, I had to convert the date to a format suitable for MySQL insert. So I used this format: String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
Then I created a function that uses this format and converts the current date and returns a string with the current date in this format. Here is the complete code.

1. Step 1: import the required classes

import java.util.Calendar;
import java.text.SimpleDateFormat;

2. Step 2: Initialize the variables

public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";

3. Step 3: create the function to get current date and convert to the above format

public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());

}

4. Step 4: Use the now function to get formatted date in MySQL format.

System.out.println("Now : " + now());

It is as easy as that. With this concept you can use it with different formats.

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

Just what I was looking for. Thank you.

View Tutorial          By: Babool at 2008-05-08 08:32:50
2. View Comment

Thank you!

View Tutorial          By: AndrĂ©s at 2014-10-31 08:31:00
3. View Comment

THANK YOU.. YO SO HELP ME :D

View Tutorial          By: xgamer at 2015-08-06 06:45:52

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J.
Error generating R.java from manifest
ERROR 1251: Client does not support authentication protocol requested by server; consider upgrading MySQL client
IIS and Tomcat - how to configure to work together
PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......
Adding your own Application icon for your J2ME application (jar file)
Creating Menus in Code using VB.net
Traversing all files in a directory using Ruby
Using Proxy to connect to URLs in Ruby
POST a form in Ruby
Reading URL content using Ruby (HTTP)
dRuby client/server mode sample program
Benchmark module in Ruby
encoding and decoding in Ruby using Base64 Module
Module Abbrev in Ruby
Standard Library Packages in Ruby

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
A Serialization Example in Java
Type Casting in Java
instanceof sample program in Java
Using One-Dimensional Arrays in Java
Method Overriding in Java
indexOf( ) and lastIndexOf( ) in Java
The java Buzzwords
Write to a file in Java - Sample Program
XML and Java - Parsing XML using Java Tutorial
Student marks calculation program in Java
Handling Fractions in Java
Calculate average sale of the week in Java
Java program to check if user input is an even number
HTTPS Java - URL Connection program. Using HttpsURLConnection
LinkedList in Java
Most Emailed Articles (in Java)
Converting a number into its equalant value in words in Java
java.lang.reflect package
Disadvantages of using Native methods in Java
What is UCS? What is ISO 10646?
Generating Your Key Pair example using keytool in Java
Operator Precedence in Java
PushbackReader sample program in Java
concat(), replace(), and trim() Strings in Java
instanceof sample program in Java
Use of - new - in Java
The Benefits of OOP
Increment and Decrement Operator
Characters in java
The continuing Revolution of java
Why java is important to the Internet