How to solve parse exception when timezone has colon? - java

I am receiving DateTime as a String from a webservice. An example of this DateTime string is: "DateTime":"2021-06-06T04:54:41-04:00".
This 2021-06-06T04:54:41-04:00 more or less matches the ISO-8601 format, so I have used this pattern to parse it: yyyy-MM-dd'T'HH:mm:ssZ. However, the colon in the timezone part of the response DateTime is causing issues. 2021-06-06T04:54:41-04:00 is giving parse exception, but 2021-06-06T04:54:41-0400 is parsing fine.
Below code should explain it better:
public void stringToDate() {
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ"; //ISO - 8601 Format
TimeZone timeZoneEST = TimeZone.getTimeZone("US/Eastern");
SimpleDateFormat sdf = new SimpleDateFormat(pattern, new Locale("en", "US"));
sdf.setLenient(false);
sdf.setTimeZone(timeZoneEST);
String timeFromWebService = "2021-06-06T04:54:41-04:00";
try {
Date parsedDate = sdf.parse(timeFromWebService); // not working because of colon in timezone part
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000); //sleep to avoid interleaving output from stacktrace (above) and syso (below)
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String timeFromWebServiceModified = "2021-06-06T04:54:41-0400"; //removed colon from timezone part
try {
Date parsedDate = sdf.parse(timeFromWebServiceModified); // working because colon is removed in timezone part
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
I want to handle this parsing without modifying the response DateTime. Any suggestions on how I can parse the original DateTime. Any suggestion on what pattern to use will be very help full.

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time, the modern API:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d'T'H:m:s[XXX][XX][X]", Locale.ENGLISH);
//Test
Stream.of(
"2021-06-06T04:54:41-04:00",
"2021-06-06T04:54:41-0400",
"2021-06-06T04:54:41-04",
"2021-06-06T04:54:41Z"
).forEach(s -> System.out.println(OffsetDateTime.parse(s, dtf)));
}
}
Output:
2021-06-06T04:54:41-04:00
2021-06-06T04:54:41-04:00
2021-06-06T04:54:41-04:00
2021-06-06T04:54:41Z
ONLINE DEMO
Check How to use OffsetDateTime in JDBC?.
Learn more about java.time, the modern Date-Time API* from Trail: Date Time.
Solution using legacy API:
SimpleDateFormat does not have a feature to specify optional patterns, the way we do, using the square bracket, with DateTimeFormatter. In this case, you can create multiple instances of SimpleDateFormat and try with each one.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
SimpleDateFormat sfdArr[] = {
new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH),
new SimpleDateFormat("y-M-d'T'H:m:sXX", Locale.ENGLISH),
new SimpleDateFormat("y-M-d'T'H:m:sX", Locale.ENGLISH)
};
String []strDateTimeArr = {
"2021-06-06T04:54:41-04:00",
"2021-06-06T04:54:41-0400",
"2021-06-06T04:54:41-04",
"2021-06-06T04:54:41Z"
};
for(String s : strDateTimeArr) {
Date date = null;
for(SimpleDateFormat sdf : sfdArr) {
try {
date = sdf.parse(s);
}catch(ParseException e) {
//...
}
}
System.out.println(date);
}
}
}
ONLINE DEMO
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Related

Convert a String (with reserved characters) to Date in Java

I want to convert a String (with reserved characters) to Date in Java
I have a string with some reserved characters in it. I am getting it from some source. Also I get the format of it from the same source. I tried to convert that string to a date but I was unable to.
The date I get:
{ts '2021-03-24 12:52:38.933'}
The format I get:
'{ts' ''yyyy-MM-dd HH:mm:ss{.SSS}[Z]'''}'
I tried with the sample code snippet but since {} are reserved characters and also ts is an invalid character for parsing, I am unable to parse it. Please help with how I can solve this.
Obviously I can do some string manipulation and convert it to a format I want but I don't want to do that.
String dateInString = "{ts '2021-03-24 12:52:38.933'}";
SimpleDateFormat sdf = new SimpleDateFormat("{ts' ''yyyy-MM-dd HH:mm:ss{.SSS}[Z]'''}", Locale.ENGLISH);
try {
Date date = sdf.parse(dateInString);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
You need to escape ' with another '.
Demo:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
String dateInString = "{ts '2021-03-24 12:52:38.933'}";
SimpleDateFormat parser = new SimpleDateFormat("'{ts '''yyyy-MM-dd HH:mm:ss.SSS'''}'", Locale.ENGLISH);
Date date = parser.parse(dateInString);
System.out.println(date);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
System.out.println(formatter.format(date));
}
}
Output:
Wed Mar 24 12:52:38 GMT 2021
2021-03-24T12:52:38.933
ONLINE DEMO
Note that the java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time, the modern Date-Time API:
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
String dateInString = "{ts '2021-03-24 12:52:38.933'}";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("'{ts '''yyyy-MM-dd HH:mm:ss.SSS'''}'", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(dateInString, parser);
System.out.println(ldt);
}
}
Output:
2021-03-24T12:52:38.933
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Query a Table based on the column as datetime2(7) from JPA

I need to query list of records between the given date range based on the column which was defined as datetime2(7) in MS SQL Server.
From the HTTP request I will be receiving startDate and endDate as below.
http://{host:port}/api/records/date?startDate=**2021-05-31T14:12:44.8020000**&endDate=**2021-05-31T14:12:44.8020000**
In the database, value in lastUpdated column is stored as 2021-05-31 14:12:44.8020000
I am trying to convert the incoming query params which is a String to java.sql.Date in the code as below
#Override
public Page<Entity> getAllRecordsWithDateRange(String startDate, String endDate) {
Page<Entity> recordsWithinDateRange = null;
String time = startDate;
String formatIn = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS";
String formatOut = "yyyy-MM-dd HH:mm:ss.SSSSSS";
SimpleDateFormat in = new SimpleDateFormat(formatIn);
SimpleDateFormat out = new SimpleDateFormat(formatOut);
Date dateIn = null;
try {
dateIn = in.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
String valueOut = out.format(dateIn);
System.out.println(">>> " + valueOut);
Pageable page = PageRequest.of(0,5000);
Date date1= null;
try {
date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").parse(valueOut);
java.util.Date utilDate = date1;
recordsWithinDateRange = repo.getAllRecordsBetweenDates(utilDate,utilDate,page);
} catch (ParseException e) {
e.printStackTrace();
}
return recordsWithinDateRange;
}
Issue I am seeing here is my actual input date is 2021-05-31T14:12:44.8020000
But, after the conversion it is incremented to a different time 2021-05-31 16:26:24.000000. So, query is returning no records from the DB.
Could someone help me to solve this issue? TIA!
The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time, the modern Date-Time API*:
datetime2 maps to TIMESTAMP ANSI SQL type or LocalDateTime in JDBC.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d'T'H:m:s.SSSSSSS", Locale.ENGLISH);
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS", Locale.ENGLISH);
String strDateTime = "2021-05-31T14:12:44.8020000";
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtfInput);
System.out.println(ldt.format(dtfOutput));
}
}
Output:
2021-05-31 14:12:44.8020000
ONLINE DEMO
Check this answer to learn how to perform JDBC operations using LocalDateTime.
Solution using legacy API:
SimpleDateFormat does not handle fraction-of-second beyond millisecond precision correctly. Check this answer to learn more about it.
You need to truncate the date-time string to millisecond precision.
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) throws ParseException {
String strDateTime = "2021-05-31T14:12:44.8020000";
strDateTime = strDateTime.substring(0, strDateTime.indexOf('.') + 4);
String formatIn = "yyyy-MM-dd'T'HH:mm:ss.SSS";
String formatOut = "yyyy-MM-dd HH:mm:ss.SSS";
SimpleDateFormat in = new SimpleDateFormat(formatIn);
SimpleDateFormat out = new SimpleDateFormat(formatOut);
System.out.println(out.format(in.parse(strDateTime)));
}
}
Output:
2021-05-31 14:12:44.802
ONLINE DEMO
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

i want to check if Date is correct like (2021-02-31 is not correct)

I want to verify if date (java.util.Date) is correct
like 2021/02/31 does not exist
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
dateFormat.setLenient(false);
try{
dateFormat.parse(dateFormat.format(date));
} catch(ParseException e) {
return false;
}
return true;
I recommend you to do it using the modern date-time API*
Use DateTimeFormatter#withResolverStyle(ResolverStyle.STRICT) to resolve a date strictly.
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDate = "2021/02/31";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd", Locale.ENGLISH)
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDate date = LocalDate.parse(strDate, dtf);
// ...
} catch (DateTimeException e) {
System.out.println(e.getMessage());
}
}
}
Output:
Text '2021/02/31' could not be parsed: Invalid date 'FEBRUARY 31'
Learn more about the modern date-time API from Trail: Date Time.
Also, note that yyyy-mm-dd is not a correct format for your date string which has / instead of - and m is used for a minute, not a month for which you have to use M.
The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API. For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
First of all your date format uses mm which represents minutes, for months it needs to be MM.
Secondly I am not sure what you meant to do with dateFormat.parse(dateFormat.format(date));. The inner part (dateFormat.format(Date)) should be used to convert a date to a string, and the outer part (dateFormat.parse(String)) is to convert a string to a date. So this seems to be a bit of a pointless operation.
I think what you meant was something like this, which should check the validity of date properly:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
try{
dateFormat.parse(dateFormat.format("2021-02-31"));
} catch(ParseException e) {
return false;
}
return true;
Having said this, using the new Java Date-Time APIs is always a better approach, so I would defer to the answer from #ArvindKumarAvinash for a more future-proof solution.

Validate a simple time date format ? (yyyy-MM-dd'T'HH:mm:ss)

I'm going to validate a simple time date format (yyyy-MM-dd'T'HH:mm:ss) as follows. This implementation works for most major validations but somehow I found some validations doesn't seems to be working.
such as if you enter 2014-09-11T03:27:54kanmsdklnasd , 2014-09-11T03:27:54234243 it doesn't validate. Can you please point out my code error?
code
String timestamp = "2014-09-11T03:27:54";
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try{
format.parse(timestamp);
LOG.info("Timestamp is a valid format");
}
catch(ParseException e)
{
return ssoResponse;
}
SimpleDateFormat.parse() (which comes from DateFormat.parse()) cannot be used for full-string validation because quoting from its javadoc:
The method may not use the entire text of the given string.
Instead you can use the DateFormat.parse(String source, ParsePosition pos) to validate.
The ParsePosition you pass is an "in-out" parameter, you can get info out of it after you call the parse() method:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
// If you set lenient to false, ranges will be checked, e.g.
// seconds must be in the range of 0..59 inclusive.
format.setLenient(false);
String timestamp = "2014-09-11T03:27:54";
ParsePosition pos = new ParsePosition(0);
format.parse(timestamp, pos); // No declared exception, no need try-catch
if (pos.getErrorIndex() >= 0) {
System.out.println("Input timestamp is invalid!");
} else if (pos.getIndex() != timestamp.length()) {
System.out.println("Date parsed but not all input characters used!"
+ " Decide if it's good or bad for you!");
} else {
System.out.println("Input is valid, parsed completely.");
}
Using JodaTime it is very Easy.
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
//from your method
String inputDateString = "2014-09-11T03:27:54kanmsdklnasd";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
boolean isValid = isValidDateFormat(inputDateString, pattern);
private boolean isValidDateFormat(String inputDateString, String format){
try {
DateTimeFormatter dtf = DateTimeFormat.forPattern(format);
dtf.parseDateTime(inputDateString);
} catch (Exception ex) {
return false;
}
return true;
}
You will get..
*Exception in thread "main" java.lang.IllegalArgumentException:
Invalid format: "2014-09-11T03:27:54kanmsdklnasd" is malformed at "kanmsdklnasd"*
java.time
The legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.
Also, check the following notice at the Home Page of Joda-Time
Joda-Time is the de facto standard date and time library for Java
prior to Java SE 8. Users are now asked to migrate to java.time
(JSR-310).
Demo using modern date-time API:
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.stream.Stream;
public class Main {
public static void main(String args[]) {
Stream.of(
"2014-09-11T03:27:54",
"2014-09-11T03:27:54kanmsdklnasd",
"2014-09-11T03:27:54234243"
).forEach(s -> {
try {
System.out.println(LocalDateTime.parse(s));
}catch(DateTimeParseException e) {
System.out.printf("The date-time string, \"%s\" is not valid.%n", s);
}
});;
}
}
Output:
2014-09-11T03:27:54
The date-time string, "2014-09-11T03:27:54kanmsdklnasd" is not valid.
The date-time string, "2014-09-11T03:27:54234243" is not valid.
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Timezone conversion for a specific datetime in java

I will be giving input date time for a timezone and the timezone for the input date time and we want the relevant DateTime in the expected timezone.
And here is my method.
convertToTimezone("03/08/2010 20:19:00 PM","Asia/Shanghai","US/Central");
The above time is the time in Asia/Shanghai. We would like to know what is the corresponding time in US/Central.
It's working fine but I am getting a 1-hour difference from the actual time.
Can I know where I am going wrong?
Here is the code:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class DateUtil {
private static String format_date = "MM/dd/yyyy HH:mm:ss a";
public static void main(String a[]) {
try {
String sourceTimezone = "Asia/Shanghai";
String destTimezone = "US/Central";
String outputExpectedTimezone = convertToTimezone("03/08/2010 20:19:00 PM", sourceTimezone, destTimezone);
System.out.println("outputExpectedTimezone :" + outputExpectedTimezone);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static String convertToTimezone(String inputDate, String inputDateTimezone, String destinationDateTimezone)
throws Exception {
String outputDate = null;
SimpleDateFormat format = new SimpleDateFormat(format_date);
format.setTimeZone(TimeZone.getTimeZone(inputDateTimezone));
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(inputDateTimezone));
calendar.setTime(format.parse(inputDate));
calendar.add(Calendar.MILLISECOND, -(calendar.getTimeZone().getRawOffset()));
calendar.add(Calendar.MILLISECOND, -calendar.getTimeZone().getDSTSavings());
calendar.add(Calendar.MILLISECOND, TimeZone.getTimeZone(destinationDateTimezone).getRawOffset());
outputDate = format.format(calendar.getTime());
return outputDate;
}
}
You shouldn't be adding anything to the calendar - that represents a specific instant in time. In fact, you don't need a calendar at all.
Instead, have two different formats, one for each time zone:
public static String convertToTimezone(String inputDate,
String inputDateTimezone,
String destinationDateTimezone)
throws Exception
{
SimpleDateFormat parser = new SimpleDateFormat(format_date);
parser.setTimeZone(TimeZone.getTimeZone(inputDateTimezone));
Date date = parser.parse(inputDate);
SimpleDateFormat formatter = new SimpleDateFormat(format_date);
formatter.setTimeZone(TimeZone.getTimeZone(outputDateTimezone));
return formatter.format(date);
}
As an aside, I'd thoroughly recommend using Joda Time instead of the built-in date/time API.
java.time
The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Also, quoted below is a notice from the home page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
Solution using java.time, the modern Date-Time API:
Since your input Date-Time does not have timezone information, parse it into a LocalDateTime
Attach the timezone of the input Date-Time with it to get a ZonedDateTime
Use the ZonedDateTime#withZoneSameInstant to convert this ZonedDateTime to the target ZonedDateTime
Return the formatted target ZonedDateTime.
Demo:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(convertToTimezone("03/08/2010 20:19:00 PM", "Asia/Shanghai", "US/Central"));
System.out.println(convertToTimezone("03/08/2010 20:19:00 PM", "Asia/Shanghai", "America/Mexico_City"));
}
static String convertToTimezone(String inputDate, String inputDateTimezone, String destinationDateTimezone) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/uuuu HH:mm:ss a", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(inputDate, dtf);
ZonedDateTime zdtInput = ldt.atZone(ZoneId.of(inputDateTimezone));
ZonedDateTime zdtDestination = zdtInput.withZoneSameInstant(ZoneId.of(destinationDateTimezone));
return zdtDestination.format(dtf);
}
}
Output:
03/08/2010 06:19:00 AM
03/08/2010 06:19:00 AM
ONLINE DEMO
Note: Avoid using the deprecated ID, US/Central. Use the standard ID, America/Mexico_City where Mexico City is the largest city in this timezone.
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Categories