Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi
I try to convert a date with timezone information into a simple date UTC.
The source Date for example is: "2017-05-12T10:15:22+02:00"
The target date with subtraction of the time zone information would be "12/05/2017 08:15:22"
It works already for one of may jobs but unfortunately not for the others.
I also wonder if there is a simpler way.
The error message is:
- Calendar cannot be resolved to a type
- GregorianCalendar cannot be resolved to a type
The code is:
// declare and init variables and data structures String datetime = ""; String d = ""; String t = ""; String add = ""; String year = ""; String month = ""; String day = ""; String finDate = ""; int a; Date myDateTime = null; String myString = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Calendar cal = new GregorianCalendar(); HashMap<String, String> hm = new HashMap<String, String>(); // put values in hash map hm.put("NUMStartDate", input_row._NUMStartDate_); hm.put("NUMEndDate", input_row._NUMEndDate_); hm.put("PublicationTimeStamp", input_row._PublicationTimeStamp_); hm.put("ModificationTimeStamp", input_row._ModificationTimeStamp_); hm.put("TimeStamp", input_row._TimeStamp_); // iterate over values in hash map and extract date and time Set set = hm.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()){ Map.Entry mentry = (Map.Entry)iterator.next(); datetime = mentry.getValue() + ""; switch (datetime) { case "null": datetime = "9999-01-01T00:00:00+00:00"; } d = datetime.substring(0, 10) + ""; t = datetime.substring(11, datetime.length()-9); add = datetime.substring(19, datetime.length()-3); a = Integer.parseInt(add); myString = d + " " + t; try { myDateTime = simpleDateFormat.parse(myString); } catch (ParseException e) { e.printStackTrace(); } cal.setTime(myDateTime); // subtract hours to date cal.add(Calendar.HOUR_OF_DAY, - a); Date date = cal.getTime(); datetime = simpleDateFormat.format( date ); d = datetime.substring(0, 10) + ""; t = datetime.substring(11, datetime.length()); year = datetime.substring(0, 4); month = datetime.substring(5, 7); day = datetime.substring(8, 10); finDate = day + "/" + month + "/" + year + " " + t; //System.out.println("Key: " + mentry.getKey() + " " + finDate + ""); switch (mentry.getKey() + "") { case "NUMStartDate": output_row._NUMStartDate_ = finDate; case "NUMEndDate": output_row._NUMEndDate_ = finDate; case "PublicationTimeStamp": output_row._PublicationTimeStamp_ = finDate; case "ModificationTimeStamp": output_row._ModificationTimeStamp_ = finDate; case "TimeStamp": output_row._TimeStamp_ = finDate; } }
That is a lot of code to format a date. This works and is a little less work....
String source = "2017-05-12T10:15:22+02:00"; source = source.substring(0, source.lastIndexOf(':'))+source.substring(source.lastIndexOf(':')+1); System.out.println(source); Date now = routines.TalendDate.parseDateInUTC("yyyy-MM-dd'T'HH:mm:ssZ", source, true); System.out.println(routines.TalendDate.formatDateInUTC("dd/MM/yyyy HH:mm:ss", now));
The +02:00 causes an issue as it should be +0200 without the ":". Remove this and the code you need is already provided by Talend.
Your code is causing the error this post is about due to this line....
Calendar cal = new GregorianCalendar();
You can fix it with this....
java.util.Calendar cal = new java.util.GregorianCalendar();
....but I suspect you will get more errors regarding HashMap, Map, Set, etc...
That is a lot of code to format a date. This works and is a little less work....
String source = "2017-05-12T10:15:22+02:00"; source = source.substring(0, source.lastIndexOf(':'))+source.substring(source.lastIndexOf(':')+1); System.out.println(source); Date now = routines.TalendDate.parseDateInUTC("yyyy-MM-dd'T'HH:mm:ssZ", source, true); System.out.println(routines.TalendDate.formatDateInUTC("dd/MM/yyyy HH:mm:ss", now));
The +02:00 causes an issue as it should be +0200 without the ":". Remove this and the code you need is already provided by Talend.
Your code is causing the error this post is about due to this line....
Calendar cal = new GregorianCalendar();
You can fix it with this....
java.util.Calendar cal = new java.util.GregorianCalendar();
....but I suspect you will get more errors regarding HashMap, Map, Set, etc...
Great, thanks for your quick answer, it works.