get ContentValues For Map - Android android.content

Android examples for android.content:ContentValues

Description

get ContentValues For Map

Demo Code


//package com.java2s;
import android.content.ContentValues;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

public class Main {
    private static SimpleDateFormat _utcIsoDateFormatter;

    public static ContentValues getContentValuesForMap(Map<String, ?> values) {
        ContentValues content = new ContentValues();

        for (String key : values.keySet()) {
            Object value = values.get(key);
            if (value == null) {
                content.putNull(key);//from   w ww.  j  a va2s. com
            } else if (value instanceof Integer) {
                content.put(key, (Integer) value);
            } else if (value instanceof Short) {
                content.put(key, (Short) value);
            } else if (value instanceof Long) {
                content.put(key, (Long) value);
            } else if (value instanceof Float) {
                content.put(key, (Float) value);
            } else if (value instanceof Double) {
                content.put(key, (Double) value);
            } else if (value instanceof Boolean) {
                content.put(key, (Boolean) value);
            } else if (value instanceof String) {
                content.put(key, (String) value);
            } else if (value instanceof Date) {
                content.put(key, getDateForDbFromDate((Date) value, false));
            }
        }

        return content;
    }

    public static String getDateForDbFromDate(Date date, boolean escape) {
        if (date != null) {
            if (escape) {
                return "'" + utcIsoDateFormatter().format(date) + "'";
            } else {
                return utcIsoDateFormatter().format(date);
            }
        } else {
            if (escape) {
                return "null";
            } else {
                return null;
            }
        }
    }

    public static SimpleDateFormat utcIsoDateFormatter() {
        if (_utcIsoDateFormatter == null) {
            TimeZone utcTimezone = TimeZone.getTimeZone("GMT");
            _utcIsoDateFormatter = new SimpleDateFormat(
                    "yyyy-MM-dd'T'HH:mm:ss'Z'");
            _utcIsoDateFormatter.setTimeZone(utcTimezone);
        }
        return _utcIsoDateFormatter;
    }
}

Related Tutorials