All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.voucherify.client.json.serializer.DateSerializer Maven / Gradle / Ivy

Go to download

Voucherify-java-sdk is a Java client (can be used in Android application as well) which was created to simplify integration with Voucherify backend (http://www.voucherify.io)

There is a newer version: 16.0.2
Show newest version
package io.voucherify.client.json.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.voucherify.client.error.VoucherifyError;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateSerializer extends JsonSerializer {

  private final DateFormat df;

  public DateSerializer(String dateFormat) {
    this.df = createDateFormat(dateFormat);
  }

  private DateFormat createDateFormat(String dateFormat) {
    try {
      return new SimpleDateFormat(dateFormat);
    } catch (Exception e) {
      throw VoucherifyError.from("Invalid date format: " + dateFormat);
    }
  }

  @Override
  public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    if (value != null) {
      gen.writeString(df.format(value));
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy