Illegal Argument Exception for setDate in java - java

I have the following code which converts the Java LocalDate to a particular format while passing as a parameter for setDate.
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
String formattedString = localDate.format(formatter);
storedProcedureCall.setDate(1, java.sql.Date.valueOf(formattedString));
But it always throws an IllegalArgumentException when I include formattedString in the argument for setDate but works fine when I do:
storedProcedureCall.setDate(2, java.sql.Date.valueOf(java.time.LocalDate.now()));
In general:
storedProcedureCall.setDate(2, java.sql.Date.valueOf(java.time.LocalDate.now())); // working
storedProcedureCall.setDate(2, java.sql.Date.valueOf(formattedString)); // not working

LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
String formattedString = localDate.format(formatter);
LocalDateTime ldt = LocalDateTime.from(LocalDate.parse(formattedString, formatter).atStartOfDay());
storedProcedureCall.setDate(1, Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()));
This should work. But if you work with standard library that use java.util.Date but not new classes like LocalDate, it is more comfortable to use old java classes.
As I understand, your goal is to set current date to storedProcedureCall. In Java any date is a long. Do not worry about local and other features, until you convert it to String. So you could solve your problem with only one line:
storedProcedureCall.setDate(1, new java.util.Date());
P.S. How to convert 24 oct 2017 to Date instance and set it to storedProcedureCall:
storedProcedureCall.setDate(1, new SimpleDateFormat("dd MMM yyyy", Locale.US).parse("24 oct 2017"));

Use this format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Your Code should be like that:
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedString = localDate.format(formatter);
storedProcedureCall.setDate(1, java.sql.Date.valueOf(formattedString));

Related

How can i convert String to Date when it has "TRT" in it

String sDate = "06.08.2020" // 06 day 08 month 2020 is year
This is the date i have in my txt file. I use them in JTable. To sort the table i convert them to date with this DateFormatter.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
And it does convert the string to date as this.
LocalDate date = LocalDate.parse(sDate,formatter);
//The date : Thu Aug 06 00:00:00 TRT 2020
Now i need to convert it like the first date 06.08.2020.
But i can't use date as input. Because i get it from JTable so i get it as String.
So i tryed this code.
String sDate1 = "Thu Aug 06 00:00:00 TRT 2020";// The date i get from JTable
LocalDate lastdate = LocalDate.parse(sDate1,formatter);
sDate1 = formatter.format(lastdate);
But i get an error as this Text 'Thu Aug 06 00:00:00 TRT 2020' could not be parsed at index 0.
So this cone not works fine : LocalDate lastdate = LocalDate.parse(sDate1,formatter);
I cant see where is the problem.
I cannot reproduce the behaviour you describe. The following code worked fine for me:
import java.util.*;
import java.text.*;
public class MyClass {
public static void main(String args[]) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String date = "06.08.2020";
Date date1 = sdf.parse(date);
String result = sdf.format(date1);
System.out.println("Date = " + result);
}
}
Output: Date = 06.08.2020
That being said, if at all possible you should switch to the new java.time.* API.
Where your code failed:
SimpleDateFormat sdf1=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "06.08.2020";
sdf1.parse(dateStr);
As you can see, the pattern of the SimpleDateFormat and that of the date string do not match and therefore, this code will throw ParseException.
How to make it work?
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
You must have already got why it worked. It worked because the pattern of the SimpleDateFormat matches with that of the dateStr string.
Can I format the Date object (i.e. date) into the original string?
Yes, just use the same format which you used to parse the original string as shown below:
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = sdf.format(date);
System.out.println(dateStr);
A piece of advice:
I recommend you switch from the outdated and error-prone java.util date-time API and SimpleDateFormat to the modern java.time date-time API and the corresponding formatting API (package, java.time.format). Learn more about the modern date-time API from Trail: Date Time.
Using the modern date-time API:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String dateStr = "06.08.2020";
LocalDate date = LocalDate.parse(dateStr, formatter);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = formatter.format(date);
System.out.println(dateStr);
I don't see any difference using the legacy API and the modern API:
That's true for this simple example but when you will need to do complex operations using date and time, you will find the modern date-time API smart and clean while the legacy API complex and error-prone.
Demo:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Given date-time string
String strDate = "Thu Aug 06 00:00:00 TRT 2020";
// Replace TRT with standard time-zone string
strDate = strDate.replace("TRT", "Europe/Istanbul");
// Define formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzzz yyyy");
// Parse the date-time string into ZonedDateTime
ZonedDateTime zdt = ZonedDateTime.parse(strDate, formatter);
System.out.println(zdt);
// If you wish, convert ZonedDateTime into LocalDateTime
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
}
Output:
2020-08-06T00:00+03:00[Europe/Istanbul]
2020-08-06T00:00

2020-04-03 20:17:46 to "yyyy-MM-dd'T'HH:mm:ss" format

Is there any way in java(java.util.* or Joda api ) to convert "2020-04-03 20:17:46" to "yyyy-MM-dd'T'HH:mm:ss"
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.parse("2020-04-03 20:17:46")
its giving java.text.parseException always
Just for the case you are using Java 8 or above, make use of java.time.
See this simple example:
public static void main(String[] args) {
// example datetime
String datetime = "2020-04-03 20:17:46";
// create a formatter that parses datetimes of this pattern
DateTimeFormatter parserDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// then parse the datetime with that formatter
LocalDateTime ldt = LocalDateTime.parse(datetime, parserDtf);
// in order to output the parsed datetime, use the default formatter (implicitly)
System.out.println(ldt);
// or format it in a totally different way
System.out.println(ldt.format(
DateTimeFormatter.ofPattern("EEE, dd. 'of' MMM 'at' hh-mm-ss a",
Locale.ENGLISH)
)
);
}
This outputs
2020-04-03T20:17:46
Fri, 03. of Apr at 08-17-46 PM
Please note that this doesn't consider any time zone or offset, it just represents a date and time consisting of the passed or parsed years, months, days, hours, minutes and seconds, nothing else.
Do not use Date/Time API from java.util.* as most of them are now outdated. Use java.time API instead.
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) throws IOException {
String strDatetime = "2020-04-03 20:17:46";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parsedDate = LocalDateTime.parse(strDatetime, formatter);
System.out.println(parsedDate);
}
}
Output:
2020-04-03T20:17:46
Learn more about DateTimeFormatter at https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Could this help you? http://tutorials.jenkov.com/java-internationalization/simpledateformat.html
First you need to parse the String with the old format, you will get a Date object. Then Create a new SimpleDateFormat with your new format, then you can format the Date object.
String dateString = "2020-04-03 20:17:46";
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
String formattedDate = output.format(date);
It do not work that way directly but if you still want to do it then, here is the process.
Create an object of SimpleDateFormat with pattern "yyyy-MM-dd HH:mm:ss")
use this to parse the string. Ultimately you are going to get date in both cases. Is there any specific reason for using T in pattern for dates which do not contain them?
Use LocalDateTime.
Timestamp timestamp = Timestamp.parse("2020-04-03 20:17:46");
LocalDateTime localDateTime = timestamp.toLocalDateTime();
System.out.println(localDateTime); // 2020-04-03T20:17:46

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

How to convert date from mm/dd/yyyy to mm dd, yyyy

I want to convert date from 07/02/2019 to July 07, 2019. My input value is 07/02/2019 I want to compare with target value July 07, 2019....Please help me on this...
public static void main(String[] args) throws ParseException {
String sDate1="07/01/2019";
java.util.Date date1=new SimpleDateFormat("MM/dd/yyyy").parse(sDate1);
System.out.println(date1);
Output:Mon Jul 01 00:00:00 IST 2019 which is not my expected value
Try this one.
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
SimpleDateFormat output = new SimpleDateFormat("MMMM dd, yyyy");
Date data = sdf.parse("07/02/2019");
String newDate = output.format(data);
System.out.println(newDate);
Here, you use:
java.util.Date date1=new SimpleDateFormat("MM/dd/yyyy").parse(sDate1);
to parse a date that comes in as String.
Then you print that date without any formatting information.
Thus the answer is quite simple: define a pattern for formatting a Date object as string! Same rules, same patterns. Just not parsing, but formatting for printing!
In other words: you already know the concept, you used a formatter to turn a String into a Date. Now simply turn that around, and provide a pattern to a formatter to turn a Date into a string!
Parse your input date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
LocalDate date = LocalDate.parse(sDate, formatter);
Similarly parse your target date
DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MMMM dd, uuuu", Locale.ENGLISH);
LocalDate targetDate = LocalDate.parse("July 07, 2019");
Or better yet, define your target date without using a string
LocalDate targetDate = LocalDate.of(2019, Month.JULY, 7);
Compare
if (date.equals(targetDate)) {
System.out.println("Same date");
}
LocalDate also have methods isBefore and isAfter.
This answer is entered from my tablet without trying the code out, so please forgive the typos.

Formating Date from parameter in BIRT

Hi so my report takes in a startDate string parameter with the value of "2017/03/18" being passed in. I need to take that and format it to be 18 March 2017.
I've tried using formatter.format(params["startDate"].value,'dd MMM yyy'); but it still doesn't work. I've also used the Format DateTime in the properties tab and it also doesn't work.
Any ideas on how I can convert it to the format I want to?
Thank you!
First You need to convert string date to Date object and then convert the date to String in the format you need.
//Before Java 8
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("dd MMMM yyyy");
String startDate = "2017/03/18";
Date sDate = sdf.parse(startDate);
String convertedDate = sdf1.format(sDate);
// Using Java 8 LocalDate & DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd MMMM yyyy");
LocalDate ldateTime = LocalDate.parse(startDate,formatter);
String formattedDate = ldateTime.format(formatter1);

Categories