We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Timestamp Java

bhession
bhession asked
on
Medium Priority
967 Views
Last Modified: 2012-05-11
Hi,
I know PHP does a timestamp of this format 110318224839 which is essentiall "ymdhms". Does Java have this? Ideally i want this date format sent to my database. I would imagine there is a simple way to do this I am just not familiar with it.
Comment
Watch Question

Raheman M. AbdulMessaging and Directory Services
CERTIFIED EXPERT

Commented:
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
java stores a Date simply as a long, you can format the date as a string anyway you requitre
http://helpdesk.objects.com.au/java/how-do-i-format-a-date-as-a-string
CERTIFIED EXPERT

Commented:

Open in new window

import java.sql.*;
import java.util.Calendar;

public class JavaTimestampCurrentTimestampExample
{
  public static void main(String[] args) throws Exception
  {
    // (1) connect to the database (mysql)
    String myDriver = "org.gjt.mm.mysql.Driver";
    String myUrl = "jdbc:mysql://localhost/date_time_tests";
    Class.forName(myDriver);
    Connection connection = DriverManager.getConnection(myUrl, "root", "");

    // (2) create a java timestamp object that represents the current time (i.e., a "current timestamp")
    Calendar calendar = Calendar.getInstance();
    java.sql.Timestamp ourJavaTimestampObject = new java.sql.Timestamp(calendar.getTime().getTime());
   
    // (3) create our java timestamp insert statement
    String sqlTimestampInsertStatement = "INSERT INTO datetests (timestamp2) VALUES (?)";
    PreparedStatement preparedStatement = connection.prepareStatement(sqlTimestampInsertStatement);
    preparedStatement.setTimestamp(1, ourJavaTimestampObject);

    // (4) execute the sql timestamp insert statement, then shut everything down
    preparedStatement.executeUpdate();
    preparedStatement.close();
    System.exit(0);
  }
}

Open in new window

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Messaging and Directory Services
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Objects,
 I tried below code from you. Threw and error below.

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
      at java.text.DateFormat.format(DateFormat.java:281)
      at java.text.Format.format(Format.java:140)
      at Date.main(Date.java:14)

marahman3001, dfke, still looking at yours

import java.text.DateFormat;
import java.text.SimpleDateFormat;


public class Date {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		DateFormat df = new SimpleDateFormat("yyMMddHHmmss");
		String date = df.format(new Date());
		System.out.println(date);
	


	}

}

Open in new window

Author

Commented:
Hi dfke, I want to create a very specific style string to insert. I can create a standard timestamp already. Its the format that I need.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Ideally i want this date format sent to my database.

If your timestamp is stored correctly (as some form of datetime datatype) in your database, it doesn't have a format. Timestamps are essentially numbers. It's only when you present the data that a format is required. At that time, it's a string, not a number. Only strings have formats

CERTIFIED EXPERT
Top Expert 2016

Commented:
Of course, you might  _start_ with a String, in which case you need to _parse_ a timestamp before insertion, e.g.
DateFormat df = new SimpleDateFormat("yyMMddHHmmss");
Date date = df.parse("110318224839");
preparedStatement.setTimestamp(1, new java.sql.Timestamp(date.getTime()));

Open in new window

Author

Commented:
CEHJ,
Ok then i guess I wish store the date as 110318224839 in a char field of a database. Normally I would not want to do this but I am using a database others are working on too and I cant change this without a lot of hassle.

Author

Commented:
Sorry just to be clear, creating a timestamp and tranforming it to a string of this format "110318224839"
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Top Expert 2016

Commented:
From a Date, it would be
DateFormat df = new SimpleDateFormat("yyMMddHHmmss");
String date = df.format(new Date());
preparedStatement.setString(1, date);

Open in new window

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks guys appreciate the help and advice. Finally from your comments I got something like below.

String Datestamp = new java.text.SimpleDateFormat("yyMMddHHmmss").format(new java.util.Date());

This did the trick. Sorry about the tardy reply I thought I closed this out earlier in the week.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.