Convert sql date to Joda Datetime - java

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

Related

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

date parsing in java to desire date format

I am trying to do an integration between 2 system,
so there is a datetime field in the source system which has format like
"dd-MMM-yyyy HH:mm:ss" for example- 19-JUN-2017 16:12:30
but what my target system reads is like "yyyy-MM-DD HH:mm:ss"
for example- 2017-12-04 19:14:16
As I am setting a value like
Tableset.setValue("TRANSACTIONDATE",CSVFILECOLUMN[1], 2L);
can any one tell me how can I parse the date format in CSV file here to set the parsed value in my TRANSACTIONDATE field.
Covert your date like this
String mydate="19-JUN-2017 16:12:30";
SimpleDateFormat currentFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
Date d = currentFormat.parse(mydate);
String formattedTime = newFormat.format(d);
then calculate difference

Convert LocalDateTime to Date

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

Convert date with known timezone to UTC date

Date and Time Conversion has always been my weak link. I have the following values in string format:
String date="2015-08-21 03:15" and timezone for this date is
String timeZone="GMT+05:30";
Now I need to covert this date, for which I already know the timezone, to UTC date.
If you are given time in "GMT+05:30" timezone next code will convert it to UTC timezone:
String strDate = "2015-08-21 03:15";
String timeZone="GMT+05:30";
String format = "yyyy-MM-dd HH:mmz";
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date dateStr = formatter.parse(strDate+timeZone);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String formattedDate = formatter.format(dateStr);
System.out.println("UTC datetime is: "+formattedDate);
You can try like this:
Approach 1: Using Java Date:
//Your input date string
String date="2015-08-21 03:15";
// date format your string
String format = "yyyy-MM-dd HH:mm";
//Create SimpleDateFormat instance
SimpleDateFormat sdf = new SimpleDateFormat(format);
// Convert Local Time to UTC
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//parse your input date string to UTC date
Date gmtTime = new Date(sdf.parse(date));
Approach 2: Using Joda time (recommended)
String dateString = "2015-08-21 03:15:00+5:30";
String pattern = "yyyy-MM-dd HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(dateString);
System.out.println(dateTime);
Since you only want a Java-8-solution:
String input = "2015-08-21 03:15";
String offsetInfo = "GMT+05:30";
LocalDateTime ldt =
LocalDateTime.parse(input, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"));
ZoneOffset offset =
ZoneOffset.of(offsetInfo.substring(3)); // GMT-prefix needs to be filtered out
LocalDateTime result =
ldt.atOffset(offset).withOffsetSameInstant(ZoneOffset.UTC).toLocalDateTime();
System.out.print(result); // output: 2015-08-20T21:45
A Date in java represents the number of milliseconds since 1970. This number alone has no specific time zone. This means if you create a Date with new Date() you get the current milliseconds since 1970 and if you call toString on it this value gets represented in your current locale timezone. The actual time this number represents is time zone specific. This is the reason why you can set a TimeZone on Calendar and Format classes.
To instantiate a calendar with a specific TimeZone you can do this:
public static Calendar getUtcCalendar() {
GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
}
So to convert a Date to a specific time in UTC TimeZone:
Calendar calendar = getUtcCalendar();
calendar.setTime(date);
return calendar;
You can see:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = null;
try {
//Here you say to java the initial timezone. This is the secret
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
date = sdf.parse(review);
} catch (ParseException e) {
e.printStackTrace();
}
//Here you set to your timezone
sdf.setTimeZone(TimeZone.getDefault());
System.out.println(sdf.format(date));

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

Categories