Java Date Now getCurrentDateAsNumericEncodedByteArray()

Here you can find the source of getCurrentDateAsNumericEncodedByteArray()

Description

get Current Date As Numeric Encoded Byte Array

License

Apache License

Declaration

public static byte[] getCurrentDateAsNumericEncodedByteArray() 

Method Source Code

//package com.java2s;
/*/*  w ww  .j a  v  a  2  s  . co  m*/
 * Copyright 2010 sasc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static byte[] getCurrentDateAsNumericEncodedByteArray() {
        SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
        return fromHexString(format.format(new Date()));
    }

    public static byte[] fromHexString(String encoded) {
        encoded = removeSpaces(encoded);
        if (encoded.length() == 0) {
            return new byte[0];
        }
        if ((encoded.length() % 2) != 0) {
            throw new IllegalArgumentException(
                    "Input string must contain an even number of characters: " + encoded);
        }
        final byte result[] = new byte[encoded.length() / 2];
        final char enc[] = encoded.toCharArray();
        for (int i = 0; i < enc.length; i += 2) {
            StringBuilder curr = new StringBuilder(2);
            curr.append(enc[i]).append(enc[i + 1]);
            result[i / 2] = (byte) Integer.parseInt(curr.toString(), 16);
        }
        return result;
    }

    public static String removeSpaces(String s) {
        return s.replaceAll(" ", "");
    }
}

Related

  1. getCurrentDate_YYYY_MM_DD()
  2. getCurrentDateAllformated()
  3. getCurrentDateAndTime()
  4. getCurrentDateAndTimeFormat()
  5. getCurrentDateAsNiceString()
  6. getCurrentDateAsString()
  7. getCurrentDateAsString(DateFormat df)
  8. getCurrentDateAsString(String timestampFormat)
  9. getCurrentDateByString(String datePattern)