Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read timestamp which is in a different locale

"timestamp_utc": "۲۰۱۵-۱۱-۰۲T۱۸:۴۴:۳۴+۰۰:۰۰"

is an attribute in a JSON. How do I parse this date? I tried the following piece of code.

try 
{
    return new DateTime(dateStr, DateTimeZone.UTC);
}
catch (IllegalArgumentException e)
{
    java.util.Locale locale = new java.util.Locale( "ar", "SA" );
    DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withLocale( locale );
    return formatter.parseDateTime( dateStr );
}

2015-05-11T11:31:47 Works just fine. However, ۲۰۱۵-۱۱-۰۲T۱۸:۴۴:۳۴+۰۰:۰۰ throws an IllegalArgumentException. Tried parsing the date with other locales/formats as well. No luck.

Locale list[] = DateFormat.getAvailableLocales();
        for (Locale aLocale : list) {
            try{
                DateTimeFormatter formatter = DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ssZ" ).withLocale(aLocale);
                System.out.println(formatter.parseDateTime( dateStr ));
            }
            catch(Exception e){
                System.out.println("locale " + aLocale.toString() + " error");
            }
        }

Please help me out.

like image 951
Karthick R Avatar asked Dec 09 '15 11:12

Karthick R


People also ask

How to get the current time in the timestamp format?

The time module ‘s time () method returns the current time in the timestamp format, which is nothing but the time elapsed from the epoch time, January 1, 1970. First, import the time module Next, use the time.time () method to get the timestamp Definition: This function returns the time in seconds since the epoch as a floating point number.

How to get a timestamp accurate to a second?

A timestamp is a sequence of characters or encoded information identifying when a certain event occurred, usually giving date and time of day, sometimes accurate to a small fraction of a second. (Wikipedia, August 29, 2016). In ABAP you get a timestamp accurate to a second with the statement. GET TIME STAMP FIELD DATA(ts).

Can search time ranges be used with all timestamps?

Search Time Ranges can also search all data with any and all timestamps. For details, see Use Receipt Time. If the browser used to access Sumo Logic is in a location that uses the day/month/year format instead of month/day/year, dates are presented in that format.

What is timestamp in SQL?

Introduction to SQL Timestamp Timestamp is a data type as well as function in Standard Structured Query Language (SQL) that lets us store and work with both date and time data values usually without time zones specified.


1 Answers

Adding a non-Arabic character (T) made it a non-Arabic date (I got the idea by trying to translate it in google translate). Try the below (Changed T to <space> in both input date and the pattern):

public static void main (String[] args) throws java.lang.Exception
{
    String ara = "۲۰۱۵-۱۱-۰۲ ۱۸:۴۴:۳۴+۰۰:۰۰";
    for (Locale aLocale : DateFormat.getAvailableLocales()) {
        //Just to save time, not needed.
        if(aLocale.getLanguage() != "ar") continue;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+SS:SS", aLocale);
            System.out.println(sdf.parse( ara ));
        }
        catch(Exception e){
            System.out.println("locale " + aLocale.toString() + " error");
        }
    }
}
like image 101
Vasan Avatar answered Oct 02 '22 04:10

Vasan