Android JSON Get jsonToDate(String jsonString)

Here you can find the source of jsonToDate(String jsonString)

Description

json To Date

License

Open Source License

Declaration

public static Long jsonToDate(String jsonString) 

Method Source Code

//package com.java2s;
/**//w w  w.  j a v  a2  s. c o m
 *     Copyright (C) 2009 Anders Aagaard <aagaande@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Calendar;

import java.util.TimeZone;

public class Main {
    public static Long jsonToDate(String jsonString) {
        Calendar calendar = Calendar.getInstance();
        int indexOfPlus = jsonString.indexOf("+");
        int startOfDate = 5;
        if (jsonString.startsWith("/")) {
            // LEGACY : parse /Date( strings : Some date strings are /Date(253402297140000+0100)/, new ones are Date(253402297140000+0100)
            startOfDate = 6;
            calendar.setTimeInMillis(Long.parseLong(jsonString.substring(
                    startOfDate,
                    indexOfPlus > 0 ? indexOfPlus : jsonString.length() - 2)));
        } else {
            calendar.setTimeInMillis(Long.parseLong(jsonString.substring(
                    startOfDate,
                    indexOfPlus > 0 ? indexOfPlus : jsonString.length() - 1)));
        }
        if (indexOfPlus > -1) {
            calendar.setTimeZone(TimeZone.getTimeZone("GMT-"
                    + jsonString.substring(indexOfPlus, indexOfPlus + 3)
                    + ":00"));
        }
        return calendar.getTimeInMillis();
    }
}

Related

  1. getObjectArray(JSONArray jsonArray, Class clazz)
  2. JSONToMap(JSONObject obj)
  3. jsonToByteBuffer(JSONObject jsonObj)