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.
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.
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
http://helpdesk.objects.com.au/java/how-do-i-format-a-date-as-a-string
import java.sql.*;import java.util.Calendar;
public class JavaTimestampCurrentTimest
{
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/da
Class.forName(myDriver);
Connection connection = DriverManager.getConnectio
// (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(calenda
// (3) create our java timestamp insert statement
String sqlTimestampInsertStatemen
PreparedStatement preparedStatement = connection.prepareStatemen
preparedStatement.setTimes
// (4) execute the sql timestamp insert statement, then shut everything down
preparedStatement.executeU
preparedStatement.close();
System.exit(0);
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Objects,
I tried below code from you. Threw and error below.
Exception in thread "main" java.lang.IllegalArgumentE xception: Cannot format given Object as a Date
at java.text.DateFormat.forma t(DateForm at.java:28 1)
at java.text.Format.format(Fo rmat.java: 140)
at Date.main(Date.java:14)
marahman3001, dfke, still looking at yours
I tried below code from you. Threw and error below.
Exception in thread "main" java.lang.IllegalArgumentE
at java.text.DateFormat.forma
at java.text.Format.format(Fo
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);
}
}
ASKER
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
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()));
ASKER
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.
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.
ASKER
Sorry just to be clear, creating a timestamp and tranforming it to a string of this format "110318224839"
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
From a Date, it would be
DateFormat df = new SimpleDateFormat("yyMMddHHmmss");
String date = df.format(new Date());
preparedStatement.setString(1, date);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys appreciate the help and advice. Finally from your comments I got something like below.
String Datestamp = new java.text.SimpleDateFormat ("yyMMddHH mmss").for mat(new java.util.Date());
This did the trick. Sorry about the tardy reply I thought I closed this out earlier in the week.
String Datestamp = new java.text.SimpleDateFormat
This did the trick. Sorry about the tardy reply I thought I closed this out earlier in the week.
How to define the timestamp in Java
https://www.experts-exchange.com/questions/24751924/Java-Timestamp.html?sfQueryTermInfo=1+10+30+java+timestamp