[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 908
  • Last Modified:

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.
0
bhession
Asked:
bhession
  • 5
  • 4
  • 3
  • +2
4 Solutions
 
Raheman M. AbdulCommented:
0
 
objectsCommented:
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
0
 
dfkeCommented:

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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
DateFormat df = new SimpleDateFormat("yyMMddhhmmss");
String date = df.format(new Date());
System.out.println(date);
0
 
Raheman M. AbdulCommented:
from the link:
Pattern: "yyMMddHHmmssZ"
See the Date and Time patterns section in   http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
0
 
bhessionAuthor 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

0
 
bhessionAuthor 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.
0
 
CEHJCommented:
>>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

0
 
CEHJCommented:
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

0
 
bhessionAuthor 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.
0
 
bhessionAuthor Commented:
Sorry just to be clear, creating a timestamp and tranforming it to a string of this format "110318224839"
0
 
CEHJCommented:
>>Ok then i guess I wish store the date as 110318224839 in a char field of a database.

That's a pity - it's inefficient and error-prone. In what form are you getting the dates in the first place - as String, as Date or what?
0
 
CEHJCommented:
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

0
 
objectsCommented:
> String date = df.format(new Date());

your class is called Date

you need to change that to

String date = df.format(new java.util.Date());
0
 
bhessionAuthor 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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now