Reputation: 33
please can any one help how to convert hexstring in the format "3676bb6c" in to date and time in the format "1-10-2013 11:47:44" I tried for this but i am getting 12 hours time format but i need 24 hours format the code would be like below
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class HexToDate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Print.logInfo("REV : " + revHexDate);
//String revHexDate="369E0Ec7";
String revHexDate="F52E32F8";
int decInt = hex2decimal(revHexDate);
System.out.println("11111 "+decInt);
int year = (decInt >> 26);
System.out.println("year is :"+year);
int month = (decInt >> 22) & 0x0f;
System.out.println("month is :"+month);
int date = (decInt >> 17) & 0x1f;
System.out.println("date is :"+date);
int hourOfDay = (decInt >> 12) & 0x1f;
int hrs=decInt/3600;
System.out.println("new hrsis "+hrs);
System.out.println("hourOfDay is :"+hourOfDay);
int minute = (decInt >> 6) & 0x3f;
System.out.println("minute is :"+minute);
int second = (decInt) & 0x3f;
System.out.println("second is :"+second);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, date, hourOfDay, minute, second);
//System.out.println(calendar.getTime());
SimpleDateFormat displayFormat1 = new SimpleDateFormat("ddMMYY",Locale.US);
String DDMMYY = displayFormat1.format(calendar.getTime());
System.out.println("DDMMYY is :"+DDMMYY);
SimpleDateFormat displayFormat2 = new SimpleDateFormat("hhmmss",Locale.US);
String HHMMSS = displayFormat2.format(calendar.getTime());
System.out.println("hhmmss is "+HHMMSS);
}
public static int hex2decimal(String s) {
String digits = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
}
Upvotes: 0
Views: 405
Reputation: 22156
Your problem is that you're using hh
instead of HH
to display hours. As stated here, the hh
is used to display a 12-hour format.
Instead of using
SimpleDateFormat displayFormat2 = new SimpleDateFormat("hhmmss",Locale.US);
use
SimpleDateFormat displayFormat2 = new SimpleDateFormat("HHmmss",Locale.US);
Also, you didn't have to create your own method to convert hex to decimal, you could have used, among others
long decLong = Long.parseLong("F52E32F8", 16);
as javaBeginner stated.
Upvotes: 1
Reputation: 13844
use the following way to for converting hexstring to date
Date d=new Date(Long.parseLong("F52E32F8", 16));
System.out.println(d);
output Tue Feb 17 20:07:25 IST 1970
Upvotes: 1