JodaTime IllegalArgumentException Invalid format "yyyy-MM-dd hh:mm:ss" - java

Before to post, i search this format in previous questions but i dont found it
Here is the simple code that i want to try :
String YYYYMMDD_HHMMSS = "yyyy/MM/dd HH:mm:ss";
DateTimeFormatter yyyyMMddHHmmssFormatter = DateTimeFormat.forPattern(YYYYMMDD_HHMMSS);
DateTime fromDate = DateTime.parse("2019-01-17 11:01:15", yyyyMMddHHmmssFormatter);
I got this error :
java.lang.IllegalArgumentException: Invalid format: "2019-01-17 11:01:15" is malformed at "-01-17 11:01:15"
I think, i must change something in the string format ?
Thank's for your help

You shoud use format yyyy-MM-dd HH:mm:ss for input 2019-01-17 11:01:15, yyyy/MM/dd HH:mm:ss works when the input like 2019/01/17 11:01:15.
Update
You need two format to convet:
DateTimeFormatter inputFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");
DateTime fromDate = DateTime.parse("2019-01-17 11:01:15", inputFormat);
System.out.println(outputFormat.print(fromDate));

Try changing
String YYYYMMDD_HHMMSS = "yyyy/MM/dd HH:mm:ss";
to
String YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";

Related

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

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

Java SimpleDateFormat use pattern with jolly character

Is there a way to parse a date with a pattern containing some "special char" or "jolly char" as separator with standard SimpleDateFormat? I want to parse my date using the patterns "yyyy MM dd", "yyyy/MM/dd", "yyyy-MM-dd" and so on..., so I'm searching something like "yyyy*MM*dd" where * is a special character meanings 'a random character'
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date d = fmt.parse(stringdate);
Use a RegEx to filter out the funny characters, then parse the filtered string?
String stringdate = "2017x01-18";
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date d = fmt.parse(Pattern.compile("(....).(..).(..)").matcher(stringdate).replaceAll("$1-$2-$3"));
you should try this
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
You can try to 'normalize' your input to desired format:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
fmt.parse(stringdate.replaceAll(" ", "-").replaceAll("/","-"));

How to format a date

I'd like to calculate the time difference between two dates, but I'm unable to parse a string representation of a date that looks like 2015-01-13 15:59:10, for example, through a formatter SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Only string dates in the form, String dateStart = "01/14/2012 09:29:58";, for example work.
How should I go about parsing 2015-01-13 15:59:10, for example through SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");?
Your format needs to match the literal you're trying to parse:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
instead of
format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Your format must match your input string. The correct format is:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
See the documentation for more details and an explanation.
to parse 2015-01-13 15:59:10 you should change the date format to below:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

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