Link to home
Start Free TrialLog in
Avatar of bhession
bhession

asked on

Timestamp Java

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.
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of dfke
dfke

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

SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bhession

ASKER

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

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.
>>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

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

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.
Sorry just to be clear, creating a timestamp and tranforming it to a string of this format "110318224839"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.