org.opentestsystem.delivery.testreg.domain.JodaDateTimeXStreamConverter.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testreg.domain.JodaDateTimeXStreamConverter.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/

package org.opentestsystem.delivery.testreg.domain;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;

/**
 * Uses ISO8601 Date Standard format (yyyy-MM-dd'T'HH:mm:ss.SSSZZ)  or yyyy-MM-dd
 * 
 */
public class JodaDateTimeXStreamConverter extends AbstractSingleValueConverter {
    DateTimeFormatter dateTimeFormatter;

    public JodaDateTimeXStreamConverter() {
        this("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
    }

    public JodaDateTimeXStreamConverter(String format) {
        switch (format) {
        case "yyyy-MM-dd":
            dateTimeFormatter = ISODateTimeFormat.yearMonthDay();
            break;
        default:
            dateTimeFormatter = ISODateTimeFormat.dateTime();
            break;
        }
    }

    public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
        return type.equals(DateTime.class);
    }

    public String toString(Object obj) {
        return ((DateTime) obj).toString(dateTimeFormatter);
    }

    @Override
    public Object fromString(String str) { //One-way converter
        return null;
    }
}