i have a date was hard coded as a string but now i'm to fetch the expired date from the database and compare with it the current time. Here is a sample of the hard coded date stored in the database:2015-02-24T13:00:00.000Z and now i have to compare this date to the current time and to check if it the date is expired. How can this be resolved by comparing the current date and time in exactly this format?
You can do it in following simple way
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = input.parse("2015-02-24T13:00:00.000Z");
System.out.println(d);
String formattedTime = output.format(d);
System.out.println(formattedTime);
The SysOut will be the like this
Tue Feb 24 13:00:00 IST 2015
2015-02-24 13:00:00
For Comparison with current date do it in following way
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sss");
Date dbDate = input.parse("2015-02-24T13:00:00.000Z");
System.out.println("dbDate"+dbDate);
System.out.println(dbDate.compareTo(new Date()));
sysout will be like this
dbDateTue Feb 24 13:00:00 IST 2015
-1
here output will be -1 means dbDate is older than current date
if output is 1 than dbDate is newer than current date.
Related
I've got Timestamp format in DB and my setEventDate method needs Date format.
So in my DAO class there is something like this :
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Timestamp dbDate = rows.getTimestamp("event_date");
String dbDateToString = new SimpleDateFormat("HH:mm").format(dbDate);
Date dbDateToDate = sdf.parse(dbDateToString);
e.setEventDate(dbDateToDate);
System.out.println(dbDateToString);
System.out.println(dbDateToDate);
I'm getting Timestamp from DB, format it to String and in next step I'm parsing it to Date. I know that it sounds weird. The result is:
String - 17:08
Date - Thu Jan 01 17:08:00 CET 1970
I don't get it :/ I need that "HH:mm" format.
You are taking a Timestamp from your DB, you need to change it a Date first
Timestamp dbDate = rows.getTimestamp("event_date");
Date d = new Date(dbDate.getTime();
now you can do your SimpleDateFormat formatting stuff
Timestamp dbDate = rows.getTimestamp("event_date");
Date d = new Date(dbDate.getTime());
String dbDateOnlyHourAndMinutes = new SimpleDateFormat("HH:mm").format(dbDate);
there you have your Hour and minutes only "Date".
You say you want to parse date but in your date format you are passing only hours (HH) and minutes (mm) format.
Check how to create your desired date format from :
What are the date formats available in SimpleDateFormat class?
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
example of a date format is: dd/MM/yyyy.
In case i did not understood correctly please explain more.
Here is an example code to convert the timestamp to your format.
ASSUMPTION: desired date format is MM:yyyy:
Timestamp stamp = new Timestamp(System.currentTimeMillis());//your timestamp goes here
Date date = new Date(stamp.getTime());
System.out.println(date);//result = Tue Mar 27 18:50:09 EEST 2018
SimpleDateFormat sdf = new SimpleDateFormat("MM:yyyy");
System.out.println(sdf.format(date)); //result = 03:2018
my problem is the following. I would like to have the german date for the string "11.11.2012".
I tried this piece of code:
String date = "11.11.2012";
SimpleDateFormat sdtF = new SimpleDateFormat("dd.mm.yyyy",Locale.GERMANY);
Date dareFormatiert = sdtF.parse(date);
System.out.println(dareFormatiert);
But it gives me the wrong format. "Wed Jan 11 00:11:00 CET 2012", instead of "11.11.2012".
Thank you, any help is appreciated.
You have to use M in you pattern, because M is the month and m is the minute!
String date = "11.11.2012";
SimpleDateFormat sdtF = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);
^^^^
Date dareFormatiert = sdtF.parse(date);
System.out.println(dareFormatiert);
For more information see the documentation of SimpleDateFormat
but i still have the same output: Sun Nov 11 00:00:00 CET 2012
Try to understand the thing when you use
SimpleDateDFormat#parse() - It parses text from a string to produce a Date.
and Date object in java always contains date along the time.
Javadoc says Date() - Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
and FYI Sun Nov 11 00:00:00 CET 2012 is equal to 11.11.2012
Edit: try this
public static Date convertUtilDateToSqlDate(java.util.Date date){
if(date != null && !(date.equals(""))) {
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
return sqlDate;
}
return null;
}
pass the util date you got above and this method shall return the sql format date then store the sqlformat date in mysql column-field of type Date
This is both a duplicate and not a duplicate!
Just please help me and don't refer me to anywhere else, cause I'm really unable to get the GMT time.
The answer seems easy but it doesn't work for me.
I don't know are the answers all aver the web wrong, or am I making a mistake?
Please take a look at this snippet and the results:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
Date date = new Date(time);
System.out.println(date);
time = System.currentTimeMillis();
date = new Date(time);
System.out.println("--\n" + date);
result :
Fri Feb 28 16:07:12 GMT+03:30 2014--
Fri Feb 28 16:07:12 GMT+03:30 2014
Both show my local time. I even printed directly the time, cause I thought maybe this is due to Date class but even those are the same (with just about 1 or 2 milliseconds difference).
Use setTimeZone() on a SimpleDateFormat to print the date in a specific timezone.
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
Date d = format.parse("28-Feb-2014 13:00:00 PST");
System.out.println(format.format(d));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(d));
Prints:
28-Feb-2014 13:00:00 PST
28-Feb-2014 21:00:00 GMT
Try the below
SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(f.format(new Date(System.currentTimeMillis())));
One way of doing it is using SimpleDateFormat and the setTimeZone method():
SimpleDateFormat sdf = new SimpleDateFormat( "..." );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); // or UTC
System.out.println( sdf.format( date ) );
Cheers,
This question already has answers here:
Is java.util.Date using TimeZone?
(5 answers)
Closed 7 years ago.
String = 26/8/2013 15:59;
I want to convert this date into GMT, however after applying the below code, I get the EEST time rather than the GMT.
DateFormat df = new SimpleDateFormat("dd/MM/yyyy h:m");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
df.parse(newDate);
Log.i(tag, df.parse(newDate).toString());
Output :
Mon Aug 26 18:59:00 EEST 2013
Whats wrong ?
Your parsing is correct, the different is just for your locale time zone that is used to display when you are making toString(). I just used formatted output to demonstrate the correct format . Here is the details example:
final String time = "26/8/2013 15:59";
TimeZone timeZone = TimeZone.getTimeZone("UTC");
final String REQUEST_DATE_FORMAT = "dd/MM/yyyy h:m";
DateFormat format = new SimpleDateFormat(REQUEST_DATE_FORMAT);
Date localDate = format.parse(time);
// localDate.toString()
// PRINT. Mon Aug 26 15:59:00 EEST 2013
Calendar cal = Calendar.getInstance(timeZone);
cal.setTime(localDate);
format.setTimeZone(timeZone);
final String utcTime = format.format(cal.getTime());
// PRINT. 26/08/2013 12:59
Nothing's really wrong. You are successfully parsing the datetime string interpreted as UTC timezone.
When printing it to log, you get what you ask for - Date.toString() returns the date formatted to current locale settings which include the timezone. The difference between UTC and EEST is 3 hours.
If you want to to format it to display some other timezone, pass it though format() of a SimpleDateFormat that is configured to the timezone you want.
I think you should use the below approach:
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
String dateAsString = outputFmt.format(time);
System.out.println(dateAsString);
I have this piece of simple code:
SimpleDateFormat sqlFormatter = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
I get this output:
2012-03-09 12:00:00
Sun Jan 01 12:00:00 EST 2012
I know is supposed to be simple, but I am hoping someone can quickly see what I am missing.
I think your pattern is a little off. I'm suprised you're not seeing an IllegalArgumentException. Try using the following pattern with lower case y's and see if that resolves your issue:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Corrected code here:
SimpleDateFormat sqlFormatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println(temp);
Date last = sqlFormatter.parse(temp);
System.out.println(last);
You should have SimpleDateFormat instead of SimpleDateFormatter and for years you give yyyy instead of YYYY.
Once I corrected your format String - Y is not allowed, you need y - (and the typo already mentioned) it worked fine:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
>2012-03-09 12:00:00
>Fri Mar 09 12:00:00 EST 2012
You need to use 'yyyy' and not 'YYYY'
Here is the output
2012-03-09 12:00:00
Fri Mar 09 12:00:00 IST 2012
for the pattern
yyyy-MM-dd HH:mm:ss