ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
There is a strange problem when reading date values from excel, date values are interpreted in numerical format rather than date format. ( For ex: 01/01/1900 is interpreted as value 2 )
Is this how, this will work ? ( Am not sure )
So, I wrote a converter to change numbers to date format
Code:
float val = Float.parseFloat(temp);
long milliSeconds = (long) val;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
Date resultDate = new Date(milliSeconds);
dateFormat.format(resultDate);
return dateFormat.toString();
But this doesn't work and returns "01/01/1970" always.
but it still returns only "01/01/1970" and nothing else whatever be the value
Here is the code snippet
Code:
recv_arg = 39723.0
DEF_DATE = MM/dd/yyyy
Code:
float value = Float.parseFloat(recv_arg);
long milliSec = (long) value;
SimpleDateFormat dateFormatSrc = new SimpleDateFormat(DEF_DATE);
Date resultDate = new Date(milliSec);
return dateFormatSrc.format(resultDate);
1. "1/1/1970" means you've got a date value of "0".
2. This means that your long variable "milliSec" is probably zero.
3. Which, in turn, means that you might not even have initialized "float val" correctly.
SUGGESTIONS:
1. Double check if the "millSec" argument in "new Date(milliSec)" should even be a "long" (I don't recall. I think it's right, but it's worth double-checking).
2. Step through your code under a debugger (or use "System.out.println()"), and make sure "val", "milliSec" and your other variables are even being initialized correctly.
@paulsm32: there is nothing wrong with the code. The date "1/1/1970" covers a lot of values in miliseconds (including 39723).
@kshkid: you do not specify the unit, you transfor the days in miliseconds by multypling with 1000*60*60*24. (*24 to get hours, *60 to get minutes, *60 to get seconds, *1000 to get miliseconds). Something like:
Code:
...
long milliSec = (long) value;
milliSec *= 1000*60*60*24;
...
Actually they are not milliSeconds, they are the number of days since Jan 1, 1970.
I did a manual breakdown and it gives me the correct result since 01/01/1970.
Is there any function in Java, given the number of days that would return date since 01/01/1970 ? Something like if the number of days is 13, it should return 13/01/1970
I tried the following code and something strange is going on ( as far as I can see )
The value fetched from excel cell is 39724 which I would like to translate it as 10/03/2008 ( Oct 3, 2008 ), but its displaying some other date value like 2079 ( year )
Code:
public static void main(String[] args) {
long numDays =14155;
numDays *= (60 * 60 *24 * 1000);
Date date2 = new Date(numDays);
DateFormat dataformat = DateFormat.getDateInstance(DateFormat.LONG);
System.out.println(dataformat.format(date2));
int val = 39724 - 14155;
System.out.println("this:" + val);
}
I have to explicitly subtract a value of 25569 to get the expected result.
You are not correct. You said: "they are the number of days since Jan 1, 1970".
If I do a simple calculation 39724 / 365 = 108. So the year should be 1970 + 108 = 2078, as Java is telling you.
My guess is that your value is the number of days since "Jan 1, 1900" so you have to substract from that one the number of days between 1970 and 1900 = 70 * 365 = 25550. So that's why you have to 'substract' that value. I am not saying your value (25569) is completly correct, please check it in the light of what I said.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.