date_time difference output is not correct - java

I have written this java code to get the difference between system_date_time and my_date_time, when I run this code system_date_time was 2015-02-19 06:01:00, so the difference should be 24 hours = 86400 seconds, but this code is giving -2592002 as output.
So please help to fix the problem.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class A {
public static void main(String arg[]) throws ParseException {
String dateString = "2015-02-20 06:01:00"; // end_date_time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date myDate = sdf.parse(dateString);
System.out.println( (myDate.getTime() - System.currentTimeMillis()) / 1000 );
}
}

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Note the MM (MM is Month, mm is minute)
You parsed the Date wrong (so it gave you sometimes in January)

You can simply use java.sql.Timestamp for what you trying to achieve.
String dateString = "2015-02-20 06:01:00"; // end_date_time
Timestamp myTimeStamp = Timestamp.valueOf(dateString);
System.out.println( ((myTimeStamp.getTime() - System.currentTimeMillis()) / 1000));

Change your date format to
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
MM is for months mm is for minutes

Update code here, first change is use MM instead of mm in SimpleDateFormat and another change in sysout
String dateString = "2015-02-18 15:39:00"; // end_date_time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date myDate = sdf.parse(dateString);
System.out.println( ( System.currentTimeMillis()- myDate.getTime()) / 1000 );

Related

how to format date using SimpleDateFormat

I cannot format a date.
dateFormat.format() accepts a Date as argument. So I created a new Date()
It says the below Date() method is deprecated, and I get the below exception while running.
exception:
Exception in thread "main" java.lang.IllegalArgumentException at
java.util.Date.parse(Date.java:598)
public class MyDate {
public static void main(String[] args) {
Date date = new Date("2012-02-16T00:00:00.000-0500");
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss");
String stringDate = dateFormat.format(date);
System.out.println(stringDate); // how do I test this conversion??
}
}
My database has date of the format - 2012-02-16T00:00:00.000-0500
I need to convert it to string of the format : dd-MMM-yyyy HH:mm:ss
I'm using Java6
Thanks to #Andy Brown. In addition to what Andy Brown has answered, I'm posting the complete snippet
Complete Solution:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SampleDate {
public static void main(String[] args) throws ParseException {
DateFormat parseFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-03-16T00:00:00.000-0500");
String strDate = parseFormat.format(date);
System.out.println(strDate);
// if you get date of type 'java.sql.Date' directly from database cursor like
//rs.getDate("created_date"), just pass it directly to format()
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss");
String stringDate = dateFormat.format(date);
System.out.println(stringDate);
}
}
/*
Output:
2012-03-16T01:00:00.000-0400
16-Mar-2012 01:00:00
*/
you can also convert java.util.Date to java.sql.Date like this,
String dateString = "03-11-2012";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
java.util.Date date = dateFormat.parse(dateString);
java.sql.Date sqlDate = new Date(date.getTime());
// set the input param type as OracleTypes.DATE and pass the input param date as sqlDate
If you want to read in the date "2012-02-16T00:00:00.000-0500" you should probably use a SimpleDateFormat to parse it like so:
DateFormat parseFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-02-16T00:00:00.000-0500");
Along with the rest of your code this writes:
16-Feb-2012 05:00:00
The parse format pattern letters are listed in the SimpleDateFormat documentation. The T is escaped with apostrophes.
This answer assumes Java 7, or you would be using the new date & time API from Java 8

Unparseable date exception when converting a string to "yyyy-MM-dd HH:mm:ss"

Hi I am trying to convert a string 19611015 to a date formate of ("yyyy-MM-dd HH:mm:ss") before storing it into a sybase database table. I have tried the below code which gives me the error:
Unparseable date: "19611015"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.parse("19611015"));
I have been reading some long and complex solutions to this, some suggesting to use Locale. Could someone explain maybe an alternative simple solution to convert string I have to a date format I am after above. Thank you.
The date in string is in yyyyMMdd format and want to convert it into yyyy-MM-dd HH:mm:ss so use below code :
DateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = originalFormat.parse("19611015");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
Output :
1961-10-15 00:00:00
Achilles, below code might solve your problem:
package com.test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class LongToDate {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(19611015 * 1000);
String dateFormatString = "yyyy-MM-dd HH:mm:ss";
DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
String result = dateFormat.format(cal.getTime());
System.out.println(result);
//Output: 1969-12-10 15:46:18
}
}

Java Formatting Date

I am trying to get the current date and format it however i am getting an invalid month of 59. Under is the code
Code
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Date todayDate = new Date();
String formatDate = df.format(todayDate);
Output is 2013-59-07
You have used mm which means minutes. Use capital MM instead.
You can find all date and time pattern symbols on the SimpleDateFormat Javadoc page.
You need "MM" for month. "mm" is for minutes.
See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You need to use capital MM and not mm. Lower case 'mm' is for minutes and not for months.
So, your code would be :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Month is retrieved by MM, not mm.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Refer to SimpleDateFormat JavaDoc:
M Month in year
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String dt = dateFormat.format(cal.getTime());
Use MM instead of mm.

how to get date and time in java like this 02-Oct-12 12:58:20 AM

i have tried some thing like this
package com.poc;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
}
}
and my out put is : 2013-01-07 17:12:27 but want the output to be like this 2013-Jan-07 17:12:27 PM
how to do this in java?
Best Regards
your format should be
new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
07-Jan-13 07:20:02 PM
new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
07-Jan-13 19:20:02
Your format should be
yyyy-MMM-dd hh:mm:ss a
use this instead:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss a");
See Class SimpleDateFormat
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
So MM is iterpreted as number and MMM or more is interpreted as text (i.e. month name)

Pick day, month and year out file.lastModified();

I know how to use file.lastModified();.
When I println that I get (for example): Sat Mar 17 09:24:33 GMT+01:00 2012.
But is it possible that I only get the day, month and year as numbers, for example: 17 03 2012
Is their a way to do this, maybe a filter, or an other function to get last modified date in numbers?
is file a java.io.File object?
lastModified should return time in milliseconds. You can create a Date object with the lastModified return value and the format the output with a SimpleDateFormat
Date date = new Date(file.lastModified());
System.out.println(date);
SimpleDateFormat sdf = new SimpleDateFormat("d M y");
System.out.println(sdf.format(date));
You can use SimpleDateFormat class, i.e.
package com.example.file;
import java.io.File;
import java.text.SimpleDateFormat;
public class GetFileLastModifiedExample
{
public static void main(String[] args)
{
File file = new File("\\somefile.txt");
System.out.println("Before Format : " + file.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("After Format : " + sdf.format(file.lastModified()));
}
}
Try this,
long longDate = file.lastModified();
Date date = new Date(longDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String newDate = formatter.format(date);
System.out.println("Formatted date " + newDate);
according to the documentation
File.lastModifed()
should return a long value representing the time the file was last modified, measured in milliseconds since the epoch. Yon can use the long value in conjunction with Calendar
Calendar rightNow = Calendar.getInstance()
rightNow.setTimeInMillis(longValue)
and use rightNow.get(...)
to retrive day, month and year

Categories