Java Json Parse parseDateFromJsonArray(final JsonArray array)

Here you can find the source of parseDateFromJsonArray(final JsonArray array)

Description

Parses a JsonArray to a Date .

License

Mozilla Public License

Parameter

Parameter Description
array the JsonArray of the date in the form of [year, month, day]

Exception

Parameter Description
ParseException an exception

Return

a parsed in the "dd MMMM yyyy" format

Declaration

public static Date parseDateFromJsonArray(final JsonArray array) throws ParseException 

Method Source Code

//package com.java2s;
/**//from   ww  w .j  a v  a2s.  c  o m
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import com.google.common.base.Preconditions;
import com.google.gson.JsonArray;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**
     * Parses a {@link JsonArray} to a {@link Date}.
     * @param array the {@link JsonArray} of the date in the form of [year, month, day]
     * @return a {@link Date} parsed in the "dd MMMM yyyy" format
     * @throws ParseException
     */
    public static Date parseDateFromJsonArray(final JsonArray array) throws ParseException {
        Preconditions.checkNotNull(array);

        final GregorianCalendar calendar = new GregorianCalendar(array.get(0).getAsInt(),
                array.get(1).getAsInt() - 1, array.get(2).getAsInt());
        final SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");

        return format.parse(format.format(calendar.getTime()));
    }
}

Related

  1. getMapFromGson(String json)
  2. json2Obj(String json, Class clazz)
  3. jsonString(String s)
  4. parse(JSONObject jsonobj)
  5. parse(String json, Class tClass)
  6. parseJson(String jsonText, String[] params)
  7. parseJSONDate(String _dateStr)