?
Solved

Convert time (double) to int

Posted on 2003-02-23
22
Medium Priority
?
832 Views
Last Modified: 2012-05-04
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
Comment
Question by:ahTri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
  • +2
22 Comments
 
LVL 9

Expert Comment

by:yongsing
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

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

Author Comment

by:ahTri
ID: 8005885
ohh I forget the signed and unsigned part, there is no way to specify sign or unsign in Java right ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:yongsing
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

by:
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

by:akshayxx
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

by:yongsing
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

by:yongsing
ID: 8006104
The last line should be:

int date32 = (int)date64;
0
 
LVL 9

Expert Comment

by:yongsing
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

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

Author Comment

by:ahTri
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

by:yongsing
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

by:ahTri
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

by:umangjoshi
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

by:akshayxx
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

by:yongsing
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

by:CEHJ
ID: 8007635
Listening..
0
 
LVL 86

Expert Comment

by:CEHJ
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

by:ahTri
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

by:CEHJ
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

by:ahTri
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

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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question