Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to import date and set node.

Avatar

Level 3

Hi,

 

I am importing data from a json and I am finding problem while setting date field.

I am using java util calendar and using set function.

here is the peace of code

'Calendar opCal = Calendar.getInstance() ;

if (OpDate.length() > 0) {
int ddOp = Integer.parseInt(schOpDate.substring(0,2));
int mmOp = Integer.parseInt(schOpDate.substring(3,5));
int yyyyOp = Integer.parseInt(schOpDate.substring(6));
opCal.clear();
opCal.set(yyyyOp,mmOp,ddOp);
}'
'chContent.setProperty("customDate", opCal);'


 but in some cases it setting wrong date like for 12-10-2021, its setting 13-11-2021. I dont know why its doing that.

Is there a better way of doing it ?

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@hptarora 

 

did you try like this? 

 

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+00:00'");

 

you can change DateFormat pattern as per your requirement. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 

        

 java.util.Date date = inputFormat.parse(inputDate);

      

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

calendar.setTime(date);

 

chContent.setProperty("customDate",calendar);

 

 

 

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

@hptarora 

 

did you try like this? 

 

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+00:00'");

 

you can change DateFormat pattern as per your requirement. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 

        

 java.util.Date date = inputFormat.parse(inputDate);

      

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

calendar.setTime(date);

 

chContent.setProperty("customDate",calendar);