Java Json writeMandatoryDateField(JsonGenerator generator, String fieldName, Date value)

Here you can find the source of writeMandatoryDateField(JsonGenerator generator, String fieldName, Date value)

Description

write Mandatory Date Field

License

Open Source License

Declaration

public static void writeMandatoryDateField(JsonGenerator generator, String fieldName, Date value)
            throws IOException 

Method Source Code

//package com.java2s;
/**/*  w ww.ja  va2s .com*/
 * Logspace
 * Copyright (c) 2015 Indoqa Software Design und Beratung GmbH. All rights reserved.
 * This program and the accompanying materials are made available under the terms of
 * the Eclipse Public License Version 1.0, which accompanies this distribution and
 * is available at http://www.eclipse.org/legal/epl-v10.html.
 */

import java.io.IOException;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;
import com.fasterxml.jackson.core.JsonGenerator;

public class Main {
    private static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    private static final String TIMEZONE_UTC = "UTC";

    public static void writeMandatoryDateField(JsonGenerator generator, String fieldName, Date value)
            throws IOException {
        String formattedValue = formatDate(value);

        writeMandatoryStringField(generator, fieldName, formattedValue);
    }

    public static String formatDate(Date value) {
        String formattedValue;
        if (value == null) {
            formattedValue = null;
        } else {
            formattedValue = getTimeFormat().format(value);
        }
        return formattedValue;
    }

    public static void writeMandatoryStringField(JsonGenerator generator, String fieldName, String value)
            throws IOException {
        generator.writeStringField(fieldName, value);
    }

    private static DateFormat getTimeFormat() {
        DateFormat df = new SimpleDateFormat(ISO_8601_DATE_FORMAT);
        df.setTimeZone(TimeZone.getTimeZone(TIMEZONE_UTC));
        return df;
    }
}

Related

  1. readMandatoryDateField(JsonParser jsonParser, String fieldName)
  2. requiredParams(JsonObject map, String... params)
  3. toDouble(Object input)
  4. toJsonArrayString(Iterator items)
  5. union(JsonObject... values)
  6. writeReferenceValue(JsonGenerator generator, Integer value)