This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
I'm retrieving a date in a HTML form, and I need to convert the string date into a java.util.Date so that it can be inserted into a database.
I've tried the following, but it always gives me a date in May 2008.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse("27-11-2015");
String string = "2015-11-27";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
This was user error, my code should have been:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date myDate = formatter.parse("27-11-2015");
Related
This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Closed 4 years ago.
I am facing an issue while converting dd/MM/yyyy format date to ccyy/MM/dd using Java. Can someone, please help me on this? It would be great If I get some example.
Here is my code## Example##
SimpleDateFormat dateFormat1 = new SimpleDateFormat("ddMMyyyy");
Date date1 = new Date();
LocalDate date = DateTimeFormat.forPattern("ddMMyyyy").parseLocalDate(dateFormat1.format(date1));
System.out.println("Century=" + date.getCenturyOfEra());
String usFormat = DateTimeFormat.forPattern("ccyy/MM/dd").print(date);
System.out.println(usFormat);
Thanks in advance.
Actually CCYYMMdd format is same as yyyyMMdd format since CC (century) is year (integer divide by 100) according to ISO 8601 you can read more in this post
converting dd/MM/yyyy date to ccyy/MM/dd date is simple
you can try this approach:
// define date source format
SimpleDateFormat sourceformat = new SimpleDateFormat("dd/MM/yyyy");
// define date target format that you want to convert to it
SimpleDateFormat targetformat = new SimpleDateFormat("yyyy/MM/dd");
// parse the input date as string with source format
// and then format it to the required target format
String dateAsString = "02/08/2018";
Date date = sourceformat.parse(dateAsString);
System.out.println(targetformat.format(date));
Output:
2018/08/02
Here is the solution for this
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String currentDate = dateFormat.format(date);
System.out.println("currentData::"+currentDate);
DateTime dt = new DateTime(dateFormat.parse(currentDate));
System.out.println("DT::"+dt);
DateTimeFormatter fmt = DateTimeFormat.forPattern("CCYY/MM/DD");
String str = fmt.print(dt);
System.out.println("CC Date::"+str);
This question already has answers here:
How to convert current date into string in java?
(9 answers)
Closed 6 years ago.
I want to convert the date format to string, but the string output is not coming
DateFormat currentDate = new SimpleDateFormat("dd MM yyyy ");
String currDateString = String.valueOf(currentDate);
System.out.println(currDateString);
To format current date you need to create a instance of Date which represents current date and then format it using SimpleDateFormat to string.
DateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
String currDateString = dateFormat.format(new Date());
System.out.println(currDateString);
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:
Closed 10 years ago.
Possible Duplicate:
Conversion of Date
How to convert date from m/d/yy to mm/dd/yyyy?
I get the date in a String as: 5/1/12.
I need to convert it to 05/01/2012.
Please suggest.
I used the following code:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
d_date = dateFormat.parse("5/1/12");
strDate = dateFormat.format(d_date);
Result:
05/01/0012
Expected Result:
05/01/2012
Thanks
Prasad
Try the following:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("5/1/12");
formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.format(date);
The following links should prove helpful for more complicated tasks:
Oracle SimpleDateFormat Docs
Example code
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);