Java Date Start getStringDate(byte[] block, int pos)

Here you can find the source of getStringDate(byte[] block, int pos)

Description

get String Date

License

Open Source License

Declaration

public static long getStringDate(byte[] block, int pos) 

Method Source Code

//package com.java2s;
/*/*w  w  w . ja  va 2s.  c o  m*/
Copyright (C) 2006-2007 loopy project (http://loopy.sourceforge.net)
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

import java.util.Calendar;
import java.util.TimeZone;

public class Main {
    public static long getStringDate(byte[] block, int pos) {
        int i = pos - 1;
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, toInt(block, i, 4));
        cal.set(Calendar.MONTH, toInt(block, (i = i + 4), 2) - 1);
        cal.set(Calendar.DATE, toInt(block, (i = i + 2), 2));
        cal.set(Calendar.HOUR_OF_DAY, toInt(block, (i = i + 2), 2));
        cal.set(Calendar.MINUTE, toInt(block, (i = i + 2), 2));
        cal.set(Calendar.SECOND, toInt(block, (i = i + 2), 2));
        cal.set(Calendar.MILLISECOND, toInt(block, (i = i + 2), 2) * 10);
        cal.setTimeZone(TimeZone.getTimeZone(getGMTpos(block[i + 2])));
        return cal.getTimeInMillis();
    }

    private static int toInt(byte[] block, int pos, int len) {
        try {
            return Integer.parseInt(new String(block, pos, len));
        } catch (Exception ex) {
            return 0;
        }
    }

    private static String getGMTpos(byte b) {
        if (0 == b) {
            return "GMT";
        }

        StringBuffer buf = new StringBuffer("GMT");
        buf.append((b < 0) ? '-' : '+');

        int posMinutes = Math.abs(b) * 15;
        int hours = posMinutes / 60;
        int minutes = posMinutes % 60;
        buf.append(hours).append(':').append((0 == minutes) ? "00" : String.valueOf(minutes));

        return buf.toString();
    }
}

Related

  1. getStartTimeOfDate(Date date)
  2. getStartTimeOfDay(Date date)
  3. getStartTimeOfDay(Date date)
  4. getStrDateToDate(String dateStr)
  5. getStringDate()
  6. getStringInDate(String dateString)