Convert LocalDateTime to Date - java

I have a string date, and I convert it with statement below:
LocalDateTime datetime = LocalDateTime.parse(rs.getString("DateIn"), DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
Now I want to convert datetime into Date for comparison purpose, how to convert it?

Date convertedDatetime = Date.from(datetime.atZone(ZoneId.systemDefault()).toInstant());

Related

Get yesterday's starting date and time and Today's starting date and time in java

Is there any way to get start and end LocalDateTime as (yesterday's date 00:00:00)~(current date 23:59:59) and need to return LocalDateTime and not String after formatting in java.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalDateTime endDate = LocalDateTime.MAX;
endDate = LocalDateTime.parse(endDate.format(formatter), formatter);
this do not return date and endDate is not string so I think not good idea
Are you looking to :
LocalDateTime now = LocalDateTime.now();
LocalDateTime startYesterday = now.minusDays(1).with(LocalTime.MIN); // 2020-03-23T00:00
LocalDateTime endToday = now.with(LocalTime.MAX); // 2020-03-24T23:59:59.999999999

How to convert String yyyy-MM-ssThh-mm-ss to LocalDataTime yyyy-MM-ss hh-mm-ss?

As in title I have a question. I need to parse LocalDataTime yyyy-MM-ssThh-mm-ss to LocalDataTime yyyy-MM-ss hh-mm-ss but when I do
String beforeConversionStartDate = "2020-01-24T00:06:56";
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDataTime parsedDate = LocalDateTime.parse(beforeConversionStartDate,formatter);
Then my output is still yyyy-MM-ddTHH:mm:ss
So my question is how to remove this "T" from LocalDataTime if parser doesn't work properly?
You are confusing between parse and format method.
First, you want to parse your input to a LocalDateTime instance
Then, you format this instance according to your formatter
String beforeConversionStartDate = "2020-01-24T00:06:56";
LocalDateTime parsedDate = LocalDateTime.parse(beforeConversionStartDate);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = parsedDate.format(formatter);
LocalDateTime (Java Platform SE 8 )
This function
LocalDateTime.parse(CharSequence text, DateTimeFormatter formatter);
takes in a string with formatted in pattern defined by formatter and returns a LocalDateTime Object, however the returned object is unaware of what what formatter was used.
Hence, you will have to format it after you have obtained the parsed your string:
String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
String newDateTime = formatter.format(dateTime);

Joda Date Format Issue

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

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

Getting ISO8601 from JodaTime with milliseconds

I want to convert my date "2013-03-04T23:00:00" to "yyyy-MM-dd'T'HH:mm:ss.SSSZ" but I keep getting "2013-03-04'T'23:00:00.000+0000". Any help?
item.getEnd() is a XMLGregorianCalendar by the way.
Calendar calendar = item.getEnd().toGregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = calendar.getTime();
DateTime iso8601 = new DateTime(calendar);
iso8601.toString("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
You should just be able to use the ISODateTimeFormat class that is built in to JodaTime. The example in the documentation is:
DateTime dt = new DateTime();
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
String str = fmt.print(dt);

Categories