How to convert date to hexadecimal in Java

I am not familiar with Java. I need to get the current datetime and express it as a string, for example:

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

060b08 is yymmdd: GPS date (November 8, 2006) 6 characters, hexadecimal

And 0d1908 are hhmmss: sending time, 6 characters, hexadecimal

Yymmdd: sending date (13:25:08), hexadecimal, for example: 060b08

I'm 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:*,"+ date_str.getBytes() +","+ hour_str.getBytes()+"#";
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
buf.writeBytes(content.getBytes(Charset.defaultCharset()));
channel.write(buf);

But wrong, returning:

#1:359672050130411:1:*,[B@7f07ff6a,[B@d4dd3b6#

Solution

Calendar. Gettime() returns date

Ideally, you should use calendar Get (calendar. Year) and calendar get(Calendar.HOUR_OF_DAY).

The same applies to month, day, minute and second

After getting the integer value, you can use integer Tohexstring() converts them to hexadecimal if necessary

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>