Java Date Convert dateToXdsDateString(Date date, int format)

Here you can find the source of dateToXdsDateString(Date date, int format)

Description

date To Xds Date String

License

Open Source License

Declaration

public static String dateToXdsDateString(Date date, int format) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    public static final int CCR_FORMAT = 1;
    public static final int HL7_FORMAT = 2;
    public static final int XDS_FORMAT = 3;

    public static String dateToXdsDateString(Date date, int format) {

        TimeZone tz = TimeZone.getTimeZone("GMT");
        Calendar cal = new GregorianCalendar(tz);
        cal.setTime(date);/* w  w w. ja v  a  2 s.  c om*/

        return getXdsDate(cal, format);
    }

    private static String getXdsDate(Calendar cal, int format) {

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int min = cal.get(Calendar.MINUTE);
        int sec = cal.get(Calendar.SECOND);
        String monthStr, domStr, hourStr, minStr, secStr;

        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = String.valueOf(month);
        }

        if (dom < 10) {
            domStr = "0" + dom;
        } else {
            domStr = String.valueOf(dom);
        }

        if (hour < 10) {
            hourStr = "0" + hour;
        } else {
            hourStr = String.valueOf(hour);
        }

        if (min < 10) {
            minStr = "0" + min;
        } else {
            minStr = String.valueOf(min);
        }

        if (sec < 10) {
            secStr = "0" + sec;
        } else {
            secStr = String.valueOf(sec);
        }

        String xdsDateString = null;
        switch (format) {
        case CCR_FORMAT:
            xdsDateString = year + "-" + monthStr + "-" + domStr + "T"
                    + hourStr + ":" + minStr + ":" + secStr + "Z";
            break;
        case HL7_FORMAT:
            xdsDateString = year + monthStr + domStr + "T" + hourStr + ":"
                    + minStr + ":" + secStr + "Z";
            break;
        case XDS_FORMAT:
            xdsDateString = year + monthStr + domStr + hourStr + minStr
                    + secStr;
            break;
        default:
            break;
        }
        return xdsDateString;
    }
}

Related

  1. dateToString(Date date)
  2. dateToString(Date date)
  3. dateToString(final Date date)
  4. dateToUnixTimestamp(Date date)
  5. dateToUTC(Date d)
  6. dateToXmlDateTime(final Date date)