Viewed   4.7k times

How do I can parse this date 2018-01-09T11:11:02.0+03:00 to dd.MM.yyyy hh:mm format in Android?

And what does T between 09 and 11 mean? Thanks.

I don't know how the back-end developer got this format. I am using Java.

 Answers

5

If you are using java, you can use SimpeDateFormat with patterns:

        String date = "2018-01-09T11:11:02.0+03:00";
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        SimpleDateFormat output = new SimpleDateFormat("dd.MM.yyyy hh:mm");
        Date d = null;
        try {
            d = dateformat.parse(date /*your date as String*/);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String formattedDate = output.format(d);

        Log.d("Date format", "output date :" + formattedDate);

The output is :

D/Date format: output date :09.01.2018 09:11

EDIT : Thanks to @OleV.V., for API > 26, or using ThreeTenABP we can use

DateTimeFormatter, we can do something like that

    String date = "2018-01-09T11:11:02.0+03:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern("dd.MM.yyyy hh:mm");
    LocalDate parsedDate = LocalDate.parse(date, formatter);

    String formattedDate = formatterOut.format(parsedDate);
    Log.d("Date format", "output date :" + formattedDate);
Monday, September 19, 2022
 
jqr
 
jqr
2

As you're interested in a Date object and the JSON occurs to me to be a unix timestamp.
Therefore I'd recommend you the Date(long milliseconds) constructor :)

private Date JSONTarihConvert(String tarih) throws ParseException{
    long timestamp = getTimeStampFromTarih(tarih);
    return new Date(timestamp);
}

Where getTimeStampFromTarih extracts the milliseconds before the occurrence of "+"

Sunday, December 25, 2022
1

Do not parse your date into the Arabic it will give you error alwayz besides try as below by setting the Locale ENGLISH only.

final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
Sunday, December 11, 2022
2

The time seems to also have the timezone there so I would do something like this:

String timeString = json.substring(json.indexOf("(") + 1, json.indexOf(")"));
String[] timeSegments = timeString.split("\+");
// May have to handle negative timezones
int timeZoneOffSet = Integer.valueOf(timeSegments[1]) * 36000; // (("0100" / 100) * 3600 * 1000)
int millis = Integer.valueOf(timeSegments[0]);
Date time = new Date(millis + timeZoneOffSet);
Wednesday, November 30, 2022
 
aterai
 
45

I tried it with 30/01/2010

SUBSTITUTE(A1,".","/")

and then I put in

=TEXT(B1, "yyyy-mm-dd").

The result was expected.

I suspect the issue is the cell you are doing this too is not in date format.

For example, if I change my value to 30.01.2010 then the value is then duplicated (in the same manner you describe)

Or, you could keep it as

=TEXT(A1, "yyyy-mm-dd")

And update all the . to / with a quick macro

Option Explicit
Sub ReplaceDate()

Dim row As Integer
row = 1

Do While (Range("A" & row).Value <> "")

    Dim val As String
    val = Range("A" & row).Value

    Dim i As Integer

    Dim result As String
    result = ""

    Dim spl() As String
    spl = Split(val, ".")

    If (UBound(spl) > 0) Then


    For i = 0 To Len(val)

    Dim r As String
        result = result & Replace(Mid(val, i + 1, 1), ".", "/")
    Next i

    End If

    If result <> "" Then

        Range("A" & row).NumberFormat = "@"
        Range("A" & row).Value = result

    End If

row = row + 1
Loop

End Sub

How do I add VBA in MS Office?

Saturday, November 19, 2022
 
kaddath
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :