Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Convert time (double) to int

Posted on 2003-02-23
Medium Priority
841 Views
From the long value of System.currentTimeMillis(), is there anyway to get date, month, year, hour and minute. All of these functions are predicated.

All I want is to convert a time (long) into an 32 bit integer
DDMMYYhhmm (2402031200 for 24 of Feb 2003 12:00 pm)
0
Question by:ahTri
• 8
• 6
• 4
• +2

LVL 9

Expert Comment

ID: 8005751
The largest possible int value is 2147483647. What are you going to do when the day of month is greater than the 21st?
0

Author Comment

ID: 8005849
isn't it 4294967296 max ? how you work out the number 2147483647 ?
0

Author Comment

ID: 8005885
ohh I forget the signed and unsigned part, there is no way to specify sign or unsign in Java right ?
0

LVL 9

Expert Comment

ID: 8005908
System.out.println(Integer.MAX_VALUE); // prints 2147483647

You are treating the 32-bit int as unsigned, and that's why you get 4294967296.
0

LVL 9

Accepted Solution

yongsing earned 800 total points
ID: 8005928
Why not just use a long (64-bit) instead of int (32-bit)?

long time = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyhhmm");
String dateString = dateFormat.format(new Date(time));
long date = Long.parseLong(dateString);
System.out.println(date); // prints 2402031134
0

LVL 8

Expert Comment

ID: 8006080
its not maximum ..4294967296
maximum is 4294967295 ( 2^32 -1) (unsigned)

N bit signed integer is in the range .. (-)2^(n-1) to 2^(n-1) -1
so unsigned will have the range
0 to 2^n - 1
0

LVL 9

Expert Comment

ID: 8006100
You can also use an int, provided you treat it as unsigned:

long time = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyhhmm");
String dateString = dateFormat.format(new Date(time));
long date64 = Long.parseLong(dateString);

// convert to 32-bit unsigned int.
if (date64 > Integer.MAX_VALUE) {
date64 -= 4294967296L;
}
int date32 = (int)date32;
0

LVL 9

Expert Comment

ID: 8006104
The last line should be:

int date32 = (int)date64;
0

LVL 9

Expert Comment

ID: 8006116
>> its not maximum ..4294967296

That's true. The number of unique values you can get with 32 bits is 4294967296, and they are 0 to 4294967295.
0

LVL 8

Expert Comment

ID: 8006167
thats exactly what i meant .. difference of 'WHOLE numbers' and 'Natural numbers' ..
0

Author Comment

ID: 8006404
yongsing,

Very close now, say today string value 2402030530 (ddMMyyhhmm) long value -1892936766, which fit in 4 bytes, now how can I convert them back to an understandable date month year hour minute value ?

Thanks, this crab been confusing me whole day
0

LVL 9

Expert Comment

ID: 8006450
>> Very close now, say today string value 2402030530 (ddMMyyhhmm) long value -1892936766

How did you get 1892936766? Is this the value from System.currentTimeMillis()?

Maybe the following is what you want. I extract out the day of month, month, year, hour and minute from the time.

long time = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);

// Get the day of the month.
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

// Get the month.
int month = calendar.get(Calendar.MONTH) + 1;

// Get the last 2 digits of year.
int year = calendar.get(Calendar.YEAR) % 1000 % 100;

// Get the hour in 12-hour format.
int hour = calendar.get(Calendar.HOUR);

// Get the minute.
int minute = calendar.get(Calendar.MINUTE);
0

Author Comment

ID: 8006470
-1892936766 is the Long value from the 2402030530 string
long date64 = Long.parseLong("2402030530");

The problem now is can't this format (DDMMYYhhmm) back to date
0

LVL 1

Expert Comment

ID: 8006613
Date d = new Date(System.currentTimeMillis());
int dd = d.getDate();
int mm = d.getMonth();
int yy = d.getYear();
int hh = d.getHours();
int min = d.getMinutes();

String s = "" + dd + mm + yy + hh + min;
long l = Long.parseLong(s);
0

LVL 8

Expert Comment

ID: 8006620
>>System.currentTimeMillis(),
>>DDMMYYhhmm (2402031200 for 24 of Feb 2003 12:00 pm)
above two statements are incompatible..
for above time System.currentTimeMillis() will not give 2402031200.

System.currentTimeMillis() gives the number of milliseconds elapsed since midnight, January 1, 1970 UTC.

if u wanted the DATE MONTH and other information from  System.currentTimeMillis .. then refer to yongsings latest comment.. else things will take slight turn
0

LVL 9

Expert Comment

ID: 8006628
-1892936766 is the signed value of 2402030530.

In the first place, why do you want to convert to the DDMMYYHHMM format? Perhaps you can tell me why you want it to be in this format? I don't understand the purpose of converting to DDMMYYHHMM and then back to the original. Why not just leave the value as it is from System.currentTimeMillis().
0

LVL 86

Expert Comment

ID: 8007635
Listening..
0

LVL 86

Expert Comment

ID: 8007674
Is this what you're looking for?

String date = new java.text.SimpleDateFormat("ddMMyyhhmm").format(new Date(System.currentTimeMillis()));
System.out.println(date);
0

Author Comment

ID: 8012087
yongsing,

good question there, I have to encode the time into 4 bytes to be sent via socket and the ridiculous system design want it to be in DDMMYYhhmm, however I will sugest to change it to YYMMDDhhmm so our system still functional until year 2021 :-)

I just realise that there is noway to convert -1892936766 or 2402030530 back to normal date, only by manually extract the digit pairs .

Thanks for all of the comment guys. I think yongsing submit the information I want first.
0

LVL 86

Expert Comment

ID: 8012158
>>I think yongsing submit the information I want first.

Yes, that's fine. I don't know what the following adds though

>>
long date = Long.parseLong(dateString);
System.out.println(date); // prints 2402031134
>>

or where the negative number came from
0

Author Comment

ID: 8012229
because signed integer max is 2147483647 , and when you program system with mix language like C with unsign integer the max is 2^32-1

there for when it is casted 2402031134 long becomes -1892936766 integer , if you want the correct value , you have to change sign for it by -= 4294967296L or += 4294967296L when you cast it back to long.

Sorry for this mess, I get confused myself

0

LVL 86

Expert Comment

ID: 8012361
Yes, i see where you're coming from.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used byâ€¦
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how â€¦
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importingâ€¦
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Booleâ€¦
###### Suggested Courses
Course of the Month11 days, 23 hours left to enroll