Java Date UTC Format formatTimeStamp(byte[] btValue, int iOffset, int iLength, int iUtcOffset)

Here you can find the source of formatTimeStamp(byte[] btValue, int iOffset, int iLength, int iUtcOffset)

Description

format Time Stamp

License

Apache License

Declaration

public static String formatTimeStamp(byte[] btValue, int iOffset,
            int iLength, int iUtcOffset) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;
import java.text.*;

public class Main {
    public static SimpleDateFormat fmtDateTime = new SimpleDateFormat(
            "dd/MM/yy HH:mm:ss");

    public static String formatTimeStamp(byte[] btValue, int iOffset,
            int iLength, int iUtcOffset) {
        if ((btValue.length < iOffset + iLength) || (iLength < 9))
            return "";
        Date dtReturn = null;/*from   w w  w  .j a v  a  2 s. co m*/
        long lOffset = 0;
        if (btValue[iOffset + 2] == '+' || btValue[iOffset + 2] == '-') {
            dtReturn = new Date(getBCDValue(btValue[iOffset + 8]),
                    getBCDValue(btValue[iOffset + 7]) - 1,
                    getBCDValue(btValue[iOffset + 6]),
                    getBCDValue(btValue[iOffset + 5]),
                    getBCDValue(btValue[iOffset + 4]),
                    getBCDValue(btValue[iOffset + 3]));
            lOffset = getBCDValue(btValue[iOffset + 1]) * 3600;
            lOffset += getBCDValue(btValue[iOffset]) * 60;
            lOffset *= 1000;
            if (btValue[iOffset + 2] == '-')
                lOffset = -lOffset;
            dtReturn.setTime(dtReturn.getTime() - lOffset + iUtcOffset);
            return fmtDateTime.format(dtReturn);
        }

        dtReturn = new Date(getBCDValue(btValue[iOffset]),
                getBCDValue(btValue[iOffset + 1]) - 1,
                getBCDValue(btValue[iOffset + 2]),
                getBCDValue(btValue[iOffset + 3]),
                getBCDValue(btValue[iOffset + 4]),
                getBCDValue(btValue[iOffset + 5]));
        lOffset = getBCDValue(btValue[iOffset + 7]) * 3600;
        lOffset += getBCDValue(btValue[iOffset + 8]) * 60;
        lOffset *= 1000;
        if (btValue[iOffset + 6] == '-')
            lOffset = -lOffset;
        dtReturn.setTime(dtReturn.getTime() - lOffset + iUtcOffset);
        return fmtDateTime.format(dtReturn);
    }

    public static int getBCDValue(byte btValue) {
        int iReturn;
        iReturn = ((btValue & 0xF0) >>> 4) * 10;
        iReturn += ((btValue & 0x0F));
        return iReturn;
    }
}

Related

  1. createUTCDateFormatter(String dateFormat)
  2. FormatAsUtc(Date date)
  3. formatAsUTC(java.util.Date date, String pattern)
  4. formatDate(Date date, String pattern, boolean toUTC)
  5. formatUTC(Date date, String format)
  6. formatUtcDateIfNotNull(final Date date)
  7. formatUtcTime(long tval)
  8. getStrTimeByUTC(long utc, String format)