Joda Date Format Issue - java

I am trying to use the following code, but I am getting an error Invalid format: "12/11/2013":
String dFrom = ps.utils.gv(request, "dFrom");
String dTo = ps.utils.gv(request, "dTo");
DateTime dateFrom = new DateTime(dFrom);
DateTime dateTo = new DateTime(dTo);
int weeks = Weeks.weeksBetween(dateFrom, dateTo).getWeeks();
Could somebody please provide an example of how to format the date variable dFrom which is typically a UK formatted date such as 12/11/2013 to an ISO Date such as 2013-11-12 which I believe Joda supports.
Any help would be much appreciated :-)

If you want convert format 12/11/2013 to 2013-11-12, you can use
DateTimeFormatter dtf = DateTimeFormat.forPatter("dd/MM/yyyy"); // or MM/dd/yyyy ?
String isoDate = ISODateTimeFormat.date().print(dtf.parseDateTime("12/11/2013"));
For ISO format 2013-11-12 you can use standart date formatter:
ISODateTimeFormat::date()
DateTime date = ISODateTimeFormat.date().parseDateTime("2013-11-12");
String dateAsString = ISODateTimeFormat.date().print(date);
For format 12/11/2013 you should create your own formatter
DateTimeFormatter dtf = DateTimeFormat.forPatter("dd/MM/yyyy"); // or MM/dd/yyyy ?
DateTime date = dtf.parseDateTime("12/11/2013");
String dateAsString = dtf.print(date);

Related

Format String yyyy-mm-dd hh:mm:ss +timezone into DateTime

I need to format a String that looks like this:
"2018-07-20 18:53:46.598000 +02:00:00"
into a DateTime object like this:
20/07/2018 (HH with Timezone applied):53:46
My approach has been:
String dateTimePattern = "dd/MM/yyyy HH:mm:ss";
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(dateTimePattern);
Date feUltModDateTime = dateTimeFormat.parse(feUltMod);
feUltMod = feUltModDateTime.toString();
But I'm getting a parse error:
java.text.ParseException: Unparseable date: "2018-07-20 18:53:46.598000 +02:00:00"
java.time
DateTimeFormatter origFormatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSS XXXXX");
DateTimeFormatter desiredFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
ZoneId desiredZone = ZoneId.of("America/Fort_Nelson");
String feUltMod = "2018-07-20 18:53:46.598000 +02:00:00";
OffsetDateTime dateTime = OffsetDateTime.parse(feUltMod, origFormatter);
ZonedDateTime dateTimeWithTimeZoneApplied = dateTime.atZoneSameInstant(desiredZone);
feUltMod = dateTimeWithTimeZoneApplied.format(desiredFormatter);
System.out.println(feUltMod);
Output from this snippet is:
20/07/2018 09:53:46
Generally you need two formatters for converting a date or date-time from one format to another: one that specifies the format to convert from and one that specifies the format to convert to.
into a DateTime object like this
A date-time object doesn’t have a format, so in that respect cannot be “like this”. dateTimeWithTimeZoneApplied in the above snippet is in the specified time zone, so has the hours adjusted. After converting to this time zone I have formatted into a string in the format you mentioned, in case you wanted this (I didn’t find it clear).
I am using and recommending java.time, the modern Java date and time API. The date and time classes you were using, Date and SimpleDateFormat, are long outdated and poorly designed, it’s not worth struggling with them. Also SimpleDateFormat supports only milliseconds so can only work correctly with exactly 3 decimals on the seconds, not with the 6 decimals you have got.
Link: Oracle tutorial: Date Time explaining how to use java.time.
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date df = new Date();
String yourString = sdf.format(df);
Date parsedDate = sdf.parse(yourString);
Timestamp sqlDate = new java.sql.Timestamp(parsedDate.getTime());
The above code will give you current Timestamp.Timestamp will provide better feasibilty

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

Conversion of String with timezone to date

I have a string containing date with timezone like
2014-01-24 09.05.14 -06:00.
I need to convert this into date format as YYYYMMDD.
What is the best way in Java to convert the above string into date.
Do it like this (requires JDK 7+):
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss X");
DateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
String dateString = "2014-01-24 09.05.14 -06:00";
Date date = inputFormat.parse(dateString);
System.out.println(outputFormat.format(date));
One way of doing it is to convert the String into a Date and then use a DateFormatter (e.g. the SimpleDateFormat) to formate that date.
Take a look at this Tutorial for further information.

JodaTime String yyyy-mm-ddThh:mmss.Z to DateTime

hi i am using Joda time to convert my string dates to DateTime objects.
I currently have the following string:
2014-02-16T00:17:20.000Z
how do i convert this to a DateTime object?
I have tried:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZZ");
DateTime dt = formatter.parseDateTime("2014-02-16T00:17:20.000Z");
But i am getting the following error:
java.lang.IllegalArgumentException: Invalid format: "2014-02-16T00:17:20.000Z" is malformed at ".000Z"
Any help is greatly appreciated
For future visitors, simpler solution:
String date = "2014-02-16T00:17:20.000Z";
DateTime dateTime = new DateTime(date);
This format happens to be the ISO date time format, that DateTime uses by default. You just need
DateTime d = DateTime.parse(s);
or
DateTime d = DateTime.parse(s, ISODateTimeFormat.dateTimeParser());
Might be issue is you guys using Z(zone) in caps
i have tested below code works well
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.ENGLISH);
Date date =formatter.parse("2016-09-06T08:35:02.530GMT");
DateTime d = new DateTime(date.getTime());

Convert sql date to Joda Datetime

Trying to convert a yyyy-MM-dd format into MM-dd-yyyy using Joda.
Invalid format: "2013-02-20" is malformed at "13-02-20"
String date = "2013-02-20";
DateTimeFormatter dft = DateTimeFormat.forPattern("MM-dd-yyyy");
DateTime d2 = DateTime.parse(date, dft);
The input pattern does not match the input date String. It should be
String date = "2013-02-20";
DateTimeFormatter dft = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime d2 = DateTime.parse(date, dft);
System.out.println(d2.toString("MM-dd-yyyy"));

Categories