• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date and time componet

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an int that represents both date and time combined in secs.
I need to separate this to 2 ints
one int having just the date no time
and 2nd int having just the time no date.

Also Do I need to convert it to gregorian calender first.


Thanks for the help
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That depends on how your int is formatted into date/time. So how is it formatted?
 
Kay Ra
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is unix time seconds from jan 1st 1970


thanks for your speedy reply.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are going to need a calendar. That timestamp is implicitly for the time zone UTC and you will want the time in your local time zone, not to mention handling things like daylight savings time correctly. Use Calendar.
 
Kay Ra
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

Using Calender. should I add the individual component.


tahnks
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "individual component"? Won't you start like this:

[ May 08, 2006: Message edited by: Jeff Albertson ]
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are going to do depends on what int values you want for the separate date and time components. For example you might want the int 20060508 for the date component and the int 110138 for the time component. Or you might want something completely different.
 
Kay Ra
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

This is how I achieved it. is there a better way

long timestampInMillis = 1000L * timeinseconds // has time in seconds from
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestampInMillis);
GregorianCalendar grecal = new GregorianCalendar(cal.YEAR, cal.MONTH, cal.DAY_OF_MONTH);
Date dt = grecal.getTime();
int dateComp =(int) dt.getTime()/1000; // has the date component
int timeComp = sch.values[0] - dateComp; // has the hr:min:sec component


thanks
 
Kay Ra
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

This line in my previous mail was cut pasted incorrectly should read as the line in bold
int timeComp = sch.values[0] - dateComp; // has the hr:min:sec component

int timeComp = timeinseconds- dateComp; // has the hr:min:sec component
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at your code, I see *two* calendars (cal and grecal) and one big mistake: static constants YEAR, MONTH and DAY_OF_MONTH do not give you cal's year, month and date -- after all they are class constants! (One of the basic rules of Java style states that you never to access class constants in this way. Most IDES will warn you about this, too.) Try redoing this code with one calendar and try using the set method to reset the hour of day, minute, seconds and milliseconds to 0. That will wind the clock back to midnight.
 
Kay Ra
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jeff for your input
This what I coded but am not sure if it is correct as in the difference I am not accurately getting the time portion


import java.util.Date;
import java.io.*;
import java.util.Calendar;
import java.lang.Integer;
import java.util.GregorianCalendar;
import java.text.DateFormat;

public class parseDate {


public static void main(String[] args) {


int givenDate = 2145916800; //1/1/2038
int givenTime = 86399; //11:59:59 PM

int totalTime = 2145916800 + 86399;


long totMilliSecs = totalTime *1000L;

System.out.println("befSet" + totMilliSecs);

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(totMilliSecs);


//testing
long gottotMilliSecs = cal.getTimeInMillis();

System.out.println("aftSet" + gottotMilliSecs);

DateFormat df1 = DateFormat.getDateTimeInstance();
Date dt = cal.getTime();
System.out.println("df1 bef making hour to zero " + df1.format(dt));


cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

dt = cal.getTime();
System.out.println("df1 setting hour:mim:sec to zero " + df1.format(dt));
long SecgottotMilliSecs = cal.getTimeInMillis();
System.out.println("aftreset" + SecgottotMilliSecs);

System.out.println("diff now " + (gottotMilliSecs - SecgottotMilliSecs));

}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code looks okay, but your timestamp for 2038-01-01 is for UTC. Here my code adjusts the Calendar and DateFormat to work in that time zone. You can choose any time zone you like, including your JVM's default:
 
Kay Ra
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff very gratefull to you, Now my concepts also have gotten clearer.



thanks
 
It means our mission is in jeapordy! Quick, read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic