Java date format conversion [duplicate] - java

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);

Related

Java : converting a String to DateFormat works but not a Date object of the converted String [duplicate]

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

Turn formatted String into Date [duplicate]

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.

Format a date to a specific format in java? [duplicate]

This question already has answers here:
Setting date into "dd"-"mm"-"yy" format from "dd"-"mm"-"yyyy" in java
(4 answers)
Closed 6 years ago.
I have a date in the format 11-NOV-13
When i convert the date i get the value Sat Nov 11 00:00:00 IST 13.
But i want the converted date to be in the format Sat Nov 11 00:00:00 IST 2013.
Though i have used the dateformat it is not taking it. Can anyone point me where i am wrong?
Below is the code i have been working on
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
Change the format to:
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
You need to make the year 2 numbers:
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
You can reference the docs here.
As you are not providing the full year you need
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
System.out.println(myDate);
Change to this format:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
String formattedDate = sdf.format(myDate);

How to convert 24 hrs time to 12 hours format in java using the date from the database [duplicate]

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

Date format in java [duplicate]

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);

Categories