How to convert date to hex in java

I am not familiar in java and I need to get the current time and present it in a string, for example:

#1:135790246811221:1:*,00000000,UP,060B08,0D1908#

where 060B08 is YYMMDD: GPS date (November 8, 2006). 6 characters, in hexadecimal format.

and 0D1908 - HHMMSS: send time, 6 characters, in hexadecimal format

YYMMDD: send date (13:25:08), 6 characters, in hexadecimal, for example :: represents the value 060B08.

I am trying this code:

Calendar cal = Calendar.getInstance();
Date date = new Date();
String date_str = String.format("%02x%02x%02x", cal.getTime().getYear(), cal.getTime().getMonth(), cal.getTime().getDay());
String hour_str = String.format("%02x%02x%02x", cal.getTime().getHours(), cal.getTime().getMinutes(), cal.getTime().getSeconds());
String content = "#1:" + imei + ":1:*,00000000,UP,"+ date_str.getBytes() +","+ hour_str.getBytes()+"#";
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
buf.writeBytes(content.getBytes(Charset.defaultCharset()));
channel.write(buf);

but erroneously returns:

#1:359672050130411:1:*,00000000,UP,[B@7f07ff6a,[B@d4dd3b6#
+4
source share
2 answers

Calendar.getTime () returns a Date . Date.getYear () is deprecated, as well as Date.getHours () and most others.

Calendar.get(Calendar.YEAR) Calendar.get(Calendar.HOUR_OF_DAY).

, , .

, , ( ) Integer.toHexString()

0

:

public static void main (String[] args) throws Exception
{
    SimpleDateFormat dateF = new SimpleDateFormat("yyMMdd");
    SimpleDateFormat timeF = new SimpleDateFormat("HHmmss");
    Date date = new Date();
    String dateHex = String.format("%020x", new BigInteger(1, dateF.format(date).getBytes("UTF-8")));
    String timeHex = String.format("%020x", new BigInteger(1, timeF.format(date).getBytes("UTF-8")));
    System.out.println("#1:359672050130411:1:*,00000000,UP," + dateHex + "," + timeHex + "#");
}

:

#1:359672050130411:1:*,00000000,UP,00000000313630333237,00000000313135363130#
0

All Articles