This question already has answers here:
Java string to date conversion
(17 answers)
Closed 4 years ago.
So I have a Date variable which I formatted using the following code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = new Date();
String date = format.format(currentDate);
I then put the String 'date' into my database.
Now: I have to pull it out of the database. It comes out as, of course, a String in the format shown above.
My question is how do I turn this String into a date variable? I'm very much struggling to find the most efficient way.
Thank you.
You can use a SimpleDateFormatter to parse a String in to a Date.
Simply use DateFormat#parse:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date inputDate = new Date();
System.out.println("inputDate: " + inputDate);
String dateString = format.format(inputDate);
System.out.println("dateString: " + dateString);
Date parsedDate = format.parse(dateString);
System.out.println("parsedDate: " + parsedDate);
Sample output:
inputDate: Fri May 04 23:56:54 GMT 2018
dateString: 2018-05-04 23:56:54
parsedDate: Fri May 04 23:56:54 GMT 2018
See working code on ideone.com.
Related
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 4 years ago.
I have a user input a Date string in one format, i can parse it using the corresponding DateFormat and format it using another (DB) DateFormat to format the Date.
However, using the DB DateFormat, i cannot convert it to the corresponding Date object.
My code:
String userSpecifiedDate = "Fri Oct 12 15:28:04 UTC 2018";
SimpleDateFormat userSpecifiedDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'UTC' yyyy");
SimpleDateFormat DBDateFormat = new SimpleDateFormat("dd-MMM-yy hh.MM.ss.SSSSSS a");
Date userDateFormat = userSpecifiedDateFormat.parse(userSpecifiedDate);
System.out.println("User specified date in original format : " + userDateFormat);
// prints just fine : 'Fri Oct 12 15:28:04 PDT 2018'
String userSpecfiedDateInDBFormat = DBDateFormat.format(userDateFormat);
System.out.println("User specified Sting in DB format : " + userSpecfiedDateInDBFormat);
// prints: '12-Oct-18 03.10.04.000000 PM' (works for me, although see the minutes are wrong)
Date uss = DBDateFormat.parse(userSpecfiedDateInDBFormat);
System.out.println("User specified Date in DB format : " + uss);
// prints : 'Fri Oct 12 15:00:04 PDT 2018' ??
I was expecting it to print the date in the DB DateFormat, same as '12-Oct-18 03.10.04.000000 PM'
I need the Date in that format since i am going to use that Date Object in the JPA to compare dates in a #NamedQuery.
An RTFM question really - Java doc: docs.oracle.com/javase/8/docs/api/java/util/… The toString() method is fixed to use the "dow mon dd hh:mm:ss zzz yyyy" format
This question already has answers here:
Converting 24hour time to 12hour time?
(6 answers)
Closed 6 years ago.
I just want to convert a date from the database to 12 hours format which is originally in 24 hours format which is something like this 2015-06-11T28:28:57.000Z. In here this time format (28:25:57) seems to be the next day (ie. 2015-06-12T04:28:57).
I have tried with the following:
String date = "2015-06-11T28:28:57.000Z";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss a");
Date date = formatter.parse(date);
but I got an error like unparsable date.
Your date formatter string was incorrect, Try this:
String dateStr = "2015-06-11T28:28:57.000Z";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date date = sdfInput.parse(dateStr);
System.out.println("Date:"+date ); //Fri Jun 12 04:28:57 IST 2015
// to get 12 hour date formate date-string from provided date
SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS ");
String dateStr2 = sdfOutput.format(date);
System.out.println("dateStr2:" + dateStr2); //2015-06-12 04:28:57.000
This question already has answers here:
Java date is not preserving milliseconds in conversion with simple date format
(5 answers)
Closed 7 years ago.
Basically I'm attempting to parse date/time to Java, but having issues when trying to parse the milliseconds.
Example of data to be parsed: a[0] = 16/03/2015, a[1] = 10:00:18.120
I read in the two values and concatenate them.
Getting: dateTime = (java.lang.String) "16/03/2015 10:00:18.120"
As you can see the string has the milliseconds when i debug it. From here I parse it to SimpleDateFormat. It works- however the milliseconds are not displayed
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
String dateTime;
dateTime= a[0]+" "+a[1];
Date d = df.parse(dateTime);
Current output: d = (java.util.Date) Mon Mar 16 10:00:18 GMT 2015
Thanks for your help.
Your code is fine, but not your interpretation of the result. As correctly mentioned in one comment, the method toString() of class java.util.Date does not output the millisecond part. But the millisecond part is still part of the state of your result object. Proof:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
String dateTime = "16/03/2015 10:00:18.120";
Date d = df.parse(dateTime);
System.out.println(d); // Mon Mar 16 10:00:18 CET 2015
System.out.println(d.getTime()); // 1426496418120
System.out.println("millisecond-part=" + (d.getTime() % 1000)); // millisecond-part=120
So all is fine. You can even format your result back to a string using the same (or another instance of SimpleDateFormat - maybe with different pattern, locale and timezone).
If java.util.Date was correctly implemented as value-type then the inventors of that class would have taken care of making the output of toString() representing the whole exact state of the object but it has not happened - another example why this class is broken.
Using DateFormat.format(Date date) function might meet your requirement
Date date = new Date();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS",Locale.ENGLISH);
String dateTime;
dateTime=df.format(date);
String[] a=dateTime.split(" ");
System.out.println(a[0]+" "+a[1]);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS",Locale.ENGLISH);
String dateTime;
dateTime= "03/16/2015"+" "+"10:00:18.120";
Date d = df.parse(dateTime);
System.out.println(df.format(d));
Try this:
String[] a = new String[]{"16/03/2015", "10:00:18.120"};
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
String dateTime = a[0] + " " + a[1];
try {
Date d = df.parse(dateTime);
System.out.println(d.getTime());//Returns milliseconds
} catch (ParseException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
The result: 1426492818120
This question already has answers here:
Java date conversion [closed]
(2 answers)
Closed 9 years ago.
date from database
2013-10-26T10:31:20GMT+05:30
UI Date
Mon Feb 10 00:00:00 GMT+05:30 2014
need to convert according to Database Date
Please try this
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
ft.format(dNow);
Note: give your format to SimpleDateFormat ,it will then format as shown above.
Use following code
// This is how to get today's date in Java
Date today = new Date();// use your date here
//SimpleDateFormat example - Date with timezone information
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm 'GMT'Z '('z')'");
String date = DATE_FORMAT.format(today);
System.out.println("Today in yyyy-MM-dd'T'HH:mm 'GMT'Z '('z')' : " + date);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert seconds_since_the_beginning_of_this_epoch to date format in java..?
hi i have "1304054138" in ssboetod . I want to display it in this "21 Apr 2011 11:46:00 AM IST" format. How i can do this in java code... ?
SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy hh:mm:ss a z")
String result = sdf.format(new Date(timestamp));
if timestamp is a String, you can obtain the long version by calling Long.parseLong(string)
Like this:
// creat date format
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG);
// set time zone
dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
// create and format date
String formattedDate = dateFormat.format(new Date(Long.valueOf("1304054138")));
// write out
System.out.println(formattedDate);