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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

get the number of days from jan,1970

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
vrbhu
Asked:
vrbhu
  • 8
  • 6
  • 4
  • +2
1 Solution
 
thanassisCommented:
<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
 
vrbhuAuthor Commented:
I wanted to calculate the number of days betweeen a given days and 1'st jan 1970.in java
0
 
sciuriwareCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
akshayxxCommented:
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
 
akshayxxCommented:
and to know the curreent time in milliseconds u can use

long milsecs=System.currentTimeMillis();
0
 
CEHJCommented:
sciuriware is right
0
 
akshayxxCommented:
>>>>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
 
CEHJCommented:
Please clarify the question vrbhu. Do you want to

>>get the number of days from jan,1970

*to* a given date?
0
 
vrbhuAuthor Commented:
Thanx i ve got the answer.
finally i found out the number of millisecs n got the number of days out of it .
0
 
vrbhuAuthor Commented:
yes i want to get the number of days from jan,1970
please suggest.
0
 
akshayxxCommented:
u got the answer and still asking for suggestion ??
u have any problems now , be more specific
0
 
vrbhuAuthor Commented:
yes i want to get the number of days from jan,1970
please suggest.
0
 
vrbhuAuthor Commented:
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
 
CEHJCommented:
>>
Thanx i ve got the answer.
finally i found out the number of millisecs n got the number of days out of it .
>>

sciuriware has already given you that answer vrbhu. Please accept it and close the question.
0
 
CEHJCommented:
>>date but i m gettin exact number of days .

That's what your question asked for in the first place
0
 
vrbhuAuthor Commented:
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
 
CEHJCommented:
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
 
vrbhuAuthor Commented:
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
 
CEHJCommented:
Well vrbhu, you really ought to give sciuriware, some points. He gave the essence of this solution way back.
0
 
vrbhuAuthor Commented:
yeah thanx  sciuriware i really appreciate the solutions that u gave ..i give him 50 points more ..
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 8
  • 6
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now