Solved

# get the number of days from jan,1970

Posted on 2003-03-03
Medium Priority
224 Views
how to get the count of number of days from
a given date (say Tue Nov 12 00:00:00 GMT+05:30 2002)
0
Question by:vrbhu
[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
• 8
• 6
• 4
• +2

LVL 4

Expert Comment

ID: 8062871
<script language = javascript>
function timetill(year,month,day){
countdown=new Date(year,month-1,day);
now = new Date();
count_sec = Math.round((countdown - now) / 1000);
days = Math.floor(count_sec/(60*60*24))
hours = Math.floor((count_sec - (days*60*60*24))/(60*60))
minutes = Math.floor((count_sec- (days*60*60*24 + hours*60*60))/60)
sec = count_sec- (days*60*60*24 + hours*60*60 + minutes*60)
str = days +" days, "+ hours +" hours, "+ minutes +" miniutes, "+ sec +" seconds"
obj_count_sec_display = document.getElementById("count_sec_display");

obj_count_sec_display.innerHTML = str+" till " + month + "/" + day +"/"+year;
setTimeout("timetill(2003,01,01)",1000);
}
timetill(2003,01,01);
</script>
0

Author Comment

ID: 8063008
I wanted to calculate the number of days betweeen a given days and 1'st jan 1970.in java
0

LVL 24

Expert Comment

ID: 8063049
As time(1-1-1970) is 0L,
you might just divide the time of your given date by 1000*60*60*24 (from milliSeconds to days).
;JOOP!

0

LVL 8

Expert Comment

ID: 8063067
its as simple as following..

import java.util.Calendar;
Calendar mycal=new Calendar();
// u might want to set the locale and timezone..
//now set the values for day month year and time

mycal.set(Calendar.YEAR,2003);
mycal.set(Calendar.DAY_OF_MONTH,5);
mycal.set(Calendar.MONTH,3);
//similarly u can set the hour minute and seconds ..
//then do this
long milSecs= mycal.getTimeInMillis();

int days= milSecs/(1000*24*60*60);
0

LVL 8

Expert Comment

ID: 8063074
and to know the curreent time in milliseconds u can use

long milsecs=System.currentTimeMillis();
0

LVL 86

Expert Comment

ID: 8063253
sciuriware is right
0

LVL 8

Expert Comment

ID: 8063637
>>>>how to get the count of number of days from
a given date

that means he doenst wish to perfom just for current time .. he would like to first set a desired date .. then calculate the number of days since 1970,1,jan
custom dates can be made with java.util.Calendar.. since they made the corresponding APIs deprecatred in java.util.Date
0

LVL 86

Expert Comment

ID: 8063653
Please clarify the question vrbhu. Do you want to

>>get the number of days from jan,1970

*to* a given date?
0

Author Comment

ID: 8070343
Thanx i ve got the answer.
finally i found out the number of millisecs n got the number of days out of it .
0

Author Comment

ID: 8070459
yes i want to get the number of days from jan,1970
0

LVL 8

Expert Comment

ID: 8070483
u have any problems now , be more specific
0

Author Comment

ID: 8070745
yes i want to get the number of days from jan,1970
0

Author Comment

ID: 8070762
Acutually i tried out with getting millisecs elasped(from 1,jan ,1970)with a given date but i m gettin exact number of days .
SimpleDateFormat l_oDateFormat = null;
java.util.Date l_oDate = null;
try
{
l_oDateFormat = new SimpleDateFormat("ddMMMyy");
System.out.println("seq-num "  + l_iSeqNumber);
l_oDate = l_oDateFormat.parse("12NOV02");
int days = l_oDate.getTime() + l_lOneHour)/ (l_lOneHour * 24);
i m not getting exact value of days...

0

LVL 86

Expert Comment

ID: 8070765
>>
Thanx i ve got the answer.
finally i found out the number of millisecs n got the number of days out of it .
>>

0

LVL 86

Expert Comment

ID: 8070785
>>date but i m gettin exact number of days .

0

Author Comment

ID: 8070787
Acutually i tried out with getting millisecs elasped(from 1,jan ,1970)with a given date but i m gettin exact number of days .
SimpleDateFormat l_oDateFormat = null;
java.util.Date l_oDate = null;
try
{
l_oDateFormat = new SimpleDateFormat("ddMMMyy");
System.out.println("seq-num "  + l_iSeqNumber);
l_oDate = l_oDateFormat.parse("12NOV02");
int days = l_oDate.getTime() + l_lOneHour)/ (l_lOneHour * 24);
i m not getting exact value of days...

0

LVL 86

Accepted Solution

CEHJ earned 60 total points
ID: 8070884
import java.text.*;
import java.util.*;

public class DaysSinceJan70 {

public static void main(String[] args) throws Exception {
final int DAYS_DIVISOR = 1000*60*60*24;
SimpleDateFormat l_oDateFormat = new SimpleDateFormat("ddMMMyy");
Date d = l_oDateFormat.parse("03JAN70");
/**
* getTime() docs:
* "Returns the number of milliseconds since January 1,
* 1970, 00:00:00 GMT represented by this Date object"
*
* Since date is midnight January 3rd, prints 1
*/
System.out.println(d.getTime() / DAYS_DIVISOR);
}

}

0

Author Comment

ID: 8071432
Acutually i tried out with getting millisecs elasped(from 1,jan ,1970)with a given date but i m gettin exact number of days .
SimpleDateFormat l_oDateFormat = null;
java.util.Date l_oDate = null;
try
{
l_oDateFormat = new SimpleDateFormat("ddMMMyy");
System.out.println("seq-num "  + l_iSeqNumber);
l_oDate = l_oDateFormat.parse("12NOV02");
int days = l_oDate.getTime() + l_lOneHour)/ (l_lOneHour * 24);
i m not getting exact value of days...

0

LVL 86

Expert Comment

ID: 8074687
Well vrbhu, you really ought to give sciuriware, some points. He gave the essence of this solution way back.
0

Author Comment

ID: 8077391
yeah thanx  sciuriware i really appreciate the solutions that u gave ..i give him 50 points more ..
0

## Featured Post

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done withâ€¦
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompaâ€¦
###### Suggested Courses
Course of the Month14 days, 18 hours left to enroll