This question already has answers here:
Sql timestamp to Java date?
(2 answers)
Closed 8 years ago.
I'm trying to retrieve a Timestamp from my DB and I need to convert it to a java.util.Date
I've been following several examples I've found in stackoverflow but I keep getting the following exception:
java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
I don't know why, I've tried several ways of doing this, can you help me?
Here's my code:
//select all partitions from table "LDRS"
stmtSelect = connection.prepareCall("Select HIGH_VALUE from user_tab_partitions where table_name = 'LDRS'");
ResultSet rs = stmtSelect.executeQuery();
SimpleDateFormat myFormat = new SimpleDateFormat("dd.MM.yyyy");
while (rs.next()) {
try {
Timestamp ts = rs.getTimestamp("HIGH_VALUE");
Date date1 = myFormat.parse(ts.toString());
Date date2 = myFormat.parse(""+new Date());
// compares the current date with the partition date, if the difference between them is more than 30 days
// drops the partition
long diff = date2.getTime() - date1.getTime();
...
}
Stop converting the values to strings and back. A java.sql.Timestamp already is a java.util.Date:
long now = System.currentTimeMillis();
while (rs.next()) {
Timestamp ts = rs.getTimestamp("HIGH_VALUE");
long diff = ts.getTime() - now;
... use diff ...
}
You should only be dealing with strings when you fundamentally want to do so. In this case, "finding the difference between a stored timestamp and the current time" has nothing to do with a string representation.
Related
This question already has answers here:
java.util.Date to java.sql.Date doesn't contain Time
(4 answers)
Closed 6 years ago.
I am trying to convert java.util.Date into java.sql.Date with below function but this function is giving the time a s 00:00:00.000.
private java.sql.Date getCurrentDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}
please help me how can I get both date and time in java.sql.date.
thanks
Converting java.util.Date to java.sql.Date will lost the hour,minute and second.
So if it is possible, I suggest you use java.sql.Timestamp.
final String str = "INSERT INTO table (curTime) VALUES( ?)";
final PreparedStatement preparedStatement =connection.prepareStatement(str);
preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));
you have to use a timestamp for that its not possible with java.sql.date since it removes data related to your time.
This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I just needed a sample code block or suggestion to convert the following date string to utc time and find difference with current time in java?
date string "2016-03-21T15:58:36-04:00"
Thanks in advance
You need to format the date string in following way:
String string = "2016-03-21T15:58:36-04:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = format.parse(string);
Then you just need to use Date APIs to find the time difference.
public static long getMillisFrom(String inStrDate) {
DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
Date inDate = ISO_DATE_FORMAT.parse(inStrDate);
Date curDate = new Date();
long diffMillis = curDate().getTime() - inDate.getTime();
return diffMillis
}
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 8 years ago.
For example, I have this String: 06/10/2013 18:29:09. I want to convert this string and put it in a SQL database (as Date).
How can I convert this to an sql date, so that it could be inserted into a database? I want the hours minutes and seconds to remain as well.
I tried the following:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date javaDate = sdf.parse("06/10/2013 18:29:09");
java.sql.Date date = new java.sql.Date(javaDate.getTime());
The problem is here:
java.sql.Date date = new java.sql.Date(javaDate.getTime());
java.sql.Date stores the date part of your timestamp. If you want/need to handle both date and time, use java.sql.Timestamp instead:
java.sql.Timestamp date = new java.sql.Timestamp (javaDate.getTime());
More info:
Date vs TimeStamp vs calendar?
You will use a SimpleDateFormat object to parse the string to java.util.date and then use the getTime() method to instantiate a java.sql.Date.
String input = "06/10/2013 18:29:09";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
java.util.Date dt = sdf.parse(input);
java.sql.Date dtSql = new java.sql.Date(dt.getTime());
If you are working with String type for date input and then you want to save that in a database like MySQL, you should use an appropriate Date Format for it. There's a class called "SimpleDateFormat" which you can use for that purpose. You can find a sample in the following link, also a brief explanation on how it works. Hope it helps.
Example: http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm
Best Regards.
Here's a simple demo. In a Database table like this.
You can insert into it like this.
//the SQL statement for creating the database table
create table user(id, integer primary key, username varchar(100), date_created varchar(100));
//the java code to insert into the table created above.
try{
String date = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss").format(new Date());
String sql = "insert into user(username, date_created) values('olupotd', '"+date+"')";
int done = statement.executeUpdate(sql);
if(done > 0)
//inserted
else
//not inserted.
}catch(java.sql.SQLException e){}
Hope that helps
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a String value which I need to convert into time and save it in the MySQL.[The column's datatype is time in the database table]
The examples of String values that I might encounter to convert into time object are:
String time = "5:32 PM";
String time = "12:00 AM";
String time = "11:43 PM";
I browsed for few examples which pulled the time from the Date object but couldn't find an apt example, such as in this case to convert it from a plain string. The main reason I need to convert it to time is to save it in the mysql.
You can convert it to java.sql.Date as :
String str = "5:32 PM";
DateFormat formatter = new SimpleDateFormat("hh:mm a");
java.util.Date date = (java.util.Date)formatter.parse(str);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
If you need java.sql.Time , then :
java.sql.Time time = new java.sql.Time(date.getTime());
And then use PreparedStatement#setTime().
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I need to convert a String which is a epoch (Unix time) format to a Date class an after a String formatted (dd/MM/yyyy).
Thank you for your help !
Unix time is the number of seconds since 1 January 1970, so this should work
Date date = new Date(unixTime * 1000);
String str = new SimpleDateFormat("dd/MM/yyyy").format(date);
BTW SimpleDateFormat accepts millis as argument too, so it is possible to get the same result as
String str = new SimpleDateFormat("dd/MM/yyyy").format(unixTime * 1000);
Date date = new Date(time);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
System.out.println(formatted);