How can I convert stringified date to date? [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
how to parse output of new Date().toString()
(4 answers)
Indonesian Times in SimpleDateFormat
(1 answer)
Closed 3 years ago.
I want to convert a string date object to Date object. In my request there is a field of the form "dateCreated":"Tue Jul 30 13:41:40 WIB 2019". I need to model it to Date class. How can I achieve this?
I thought the controller will automatically cast it to Date object. It didn't work. Also, simple casting to Date object from string also didnt work. I could not find a string formatter for this also.
private Date dateCreated = new Date();
So I want to convert a stringified Date object to Date object.

If you describe your format then you can do it with Joda time
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);
Well a slightly more complex problem of how do you parse date that can be in multiple formats, can be solved
DateTimeParser[] parsers = {
DateTimeFormat.forPattern("yyyy-MM-dd HH").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd").getParser() };
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
DateTime date1 = formatter.parseDateTime( "2010-01-01" );
DateTime date2 = formatter.parseDateTime( "2010-01-01 01" );
https://www.joda.org/joda-time/
edit
From java 8 you can use
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date1 = LocalDate.from(formatter.parse("2010-01-01"));

Related

Formatting LocalDate in Java [duplicate]

This question already has answers here:
Get first and last day of month using threeten, LocalDate
(13 answers)
How to format LocalDate to yyyyMMDD (without JodaTime)
(2 answers)
Closed 3 years ago.
I defined the following format in Java :
//define format of YYYYMMDD
private final DateTimeFormatter dtf = DateTimeFormatter.BASIC_ISO_DATE;
My application fetches a certain date from the calendar:
LocalDate localDate = fetchDate(); // fetch date in format "yyyy-mm-dd"
I want to store an additional two dates in format of dtf. The startOfMonth and endOfMonth of the given localDate.
E.g. if localDate is "2019-12-12" I want to create the following two variables -
String startOfMonth = "20191201";
String endOfMonth = "20191231";
How can I do that?
LocalDate atStartOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
LocalDate atEndOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
And then you format those local dates the way you want to (i.e. with your dtf formatter)

How to convert dd/MM/yyyy format date to ccyy/MM/dd format in java? [duplicate]

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

Best way to transform date string into an date object [duplicate]

This question already has answers here:
How to parse date string to Date? [duplicate]
(6 answers)
Java string to date conversion
(17 answers)
Closed 6 years ago.
What is the best way to transform like October-2016 into an Date Object?
When I try to use Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
it throws an exception, because it cannot convert this type of date.
since the string October-2016 is containing information related to a language YOU NEED TO SPECIFY the Locale in the instance of the SimpleDateFormat
Date date = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
String st = "October-2016";
DateFormat format = new SimpleDateFormat("MMMM-yyyy");
Date date = format.parse(st);
System.out.println(date);
Both working same:
Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
Date date1 = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
System.out.println(date);
System.out.println(date1);

java - Convert date string to UTC time [duplicate]

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
}

Java how to convert UK into java.util.Date [duplicate]

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

Categories