DateTime String Pattern

What is the pattern to parse backendless DateTime? I’ve used “EE MM dd, yyyy” and various other modifications but I was still getting a DateTimeParseException. Sample date is “Mon Nov 01 00:00:00 GMT+01:00 2021”

Hello @Nkekere_Tommy_Minimann

here is the list Using Dates in Search - Backendless SDK for JavaScript API Documentation

I got this date "Mon Nov 01 00:00:00 GMT+01:00 2021”
from one of my tables. What format do I use to parse it to Monday Nov 1 2021?

What Backendless SDK (JS/Java/REST/…) do you use?

Java

Hello @Nkekere_Tommy_Minimann

We will be happy to assist you. I need to ask you a few more questions so I can understand the problem better.

You get a column from the Backendless, a column of type java.util.Date and you want to format this java.util.Date?

Please provide an example of a code where you get an exception.
Please provide the full text of the error.

Perhaps the following information will help you:
DateTimeParseException
DateTimeFormatter

String whereClause = “PosterId =’” + userId() + “’”;

    DataQueryBuilder queryBuilder = DataQueryBuilder.create();
    queryBuilder.setWhereClause(whereClause);

    try {
        Backendless.Data.of("Master").find(queryBuilder, new AsyncCallback<List<Map>>() {

            @Override
            public void handleResponse(List<Map> response) {

                try {
                  
                    String refreshedDate = Objects.requireNonNull(response.get(0).get("Refreshed").toString());  
		
                    Log.d(getClass().getName(), "refreshedDate:"+refreshedDate);
		//This is what i get from my table: "Mon Nov 01 00:00:00 GMT+01:00 2021"                    
                    //But it would have been easier for me if i got it in this format "11/01/2021 00:00:00";
		//So that i can parse it like this "mm/dd/yyyy HH:mm:ss"
                    //DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EE MM dd, yyyy");//not working
                    //DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); //not working
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EE MM dd HH:mm:ss zzz  yyyy");//not working
                    LocalDate localDateRefreshed = formatter.parse(refreshedDate,LocalDate::from);

                    Log.d(getClass().getName(), "refreshedDate2:"+localDateRefreshed.toString());
                    ZoneId zone = ZoneId.systemDefault();
                    LocalDate today = LocalDate.now(zone);
                    if(today.minusDays(7).compareTo(localDateRefreshed)>0)//refreshed more than than 7 days ago
                    {
                        shouldRefresh[0] = true;
                        holder.textViewAdvertTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_refresh_black_24dp, 0);
                    }
                }
                catch (Exception e) {
                    Log.e(getClass().getName(), "Error:" + e.toString());
                    Log.e(getClass().getName(), "Error:" + e.toString());
                }
            }

        
            @Override
            public void handleFault(BackendlessFault fault) {

                Log.e(getClass().getName(), "Error->:" + fault.toString());

            }
        });
    }
    catch (Exception e)
    {
        Log.e(getClass().getName(), "Error:" + e.toString());
        
    }

Hello @Nkekere_Tommy_Minimann

the date in your case is java.util.Date so just use the SimpleDateFormat as in the following example:

    String pattern = "mm/dd/yyyy HH:mm:ss";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat( pattern);
    System.out.println(simpleDateFormat.format( response.get(0).get("Refreshed") ));

Am still getting the same exception
DateTimeParseException: Text ‘Mon Nov 01 00:00:00 GMT+01:00 2021’ could not be parsed at index 0
But when I used “EE MM dd HH:mm:ss” the error occured at index 4

@Nkekere_Tommy_Minimann

did you remove your code and try the one I had suggested?

Yes. I did that. When I used your code the error occured at index. 0, when I Used my code it occurred at index 4

How about using the debugger to narrow down the issue?

I used a hack.It works but i dont like it

String whereClause = “PosterId =’” + userId() + “’”;

    DataQueryBuilder queryBuilder = DataQueryBuilder.create();
    queryBuilder.setWhereClause(whereClause);

    try {
        Backendless.Data.of("Master").find(queryBuilder, new AsyncCallback<List<Map>>() {

            @Override
            public void handleResponse(List<Map> response) {


                try {

                    String refreshedDate = Objects.requireNonNull(response.get(0).get("Refreshed").toString());
                    Log.d(getClass().getName(), "refreshedDate:"+refreshedDate);
		//refreshedDate :Mon Nov 01 00:00:00 GMT+01:00 2021
                    //Basically my problem is how to convert this :"Mon Nov 01 00:00:00 GMT+01:00 2021" TO this "11/01/2021 00:00:00" 

                    refreshedDate = refreshedDate.substring(3); //to remove the day of week ie "Mon"
                    Date temp = new Date(refreshedDate);
                    Format format = new SimpleDateFormat("MM/dd/yyyy");
                    String newDateString = format.format(temp);

                    Log.d(getClass().getName(), "New Date String:"+newDateString);
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd");

                    Calendar c = Calendar.getInstance();
                    c.add(Calendar.DATE,-7);
                    Log.d(getClass().getName(), "date c:"+c.getTime());
                    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
                    Date date = sdf.parse(newDateString);
                    Calendar d = Calendar.getInstance();
                    d.setTime(date);
                    Log.d(getClass().getName(), "date d:"+d.getTime());

                    if(c.compareTo(d)>0)//refreshed more than than 7 days ago
                    {
                       
                       
                        holder.textViewAdvertTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_refresh_black_24dp, 0);
                       
                    }
                    else{

                            
                    }
                                    }

         
            @Override
            public void handleFault(BackendlessFault fault) {

                Log.e(getClass().getName(), "Error->:" + fault.toString());

            }
        });

Hi @Nkekere_Tommy_Minimann !

You can use this pattern to parse your date-time string

EE MMM dd HH:mm:ss zzz yyyy

I have tried it for string “Mon Nov 01 00:00:00 GMT+01:00 2021” and it was parsed without errors.

Regards, Andriy