Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Query with Date in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-02-2010
Member
 
Join Date: Dec 2009
Posts: 296
Query with Date in java

Hello,
I am trying a program in java which is related to Date format. But i think i have a problem with date in java. Here is my code, just go through it
Code:
Calendar cal = Calendar.getInstance(Local.ENGLISH);
cal.setTimeInMillis(1000);
String date = String.function valueOf() {
    [native code]
}(cal.get(Calendar.HOUR))+":"+
                   String.function valueOf() {
    [native code]
}(cal.get(Calendar.MINUTE))+":"+
                   String.function valueOf() {
    [native code]
}(cal.get(Calendar.SECOND));
Reply With Quote
  #2  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
Re: Query with Date in java

Hello,
This is normal, because getInstance (Local) only modifies the localization (language + format) but not the time zone (which is the default). If you want to use the GMT time zone as I think you should specify it explicitly.
Code:
Calendar cal = Calendar.getInstance(TimeZone.getTimezone("GMT"));
I hope you are understanding what I am trying to say here. If you want more information on this then you can ask.
Reply With Quote
  #3  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: Query with Date in java

Hello,
When using the function getTime () of the Calendar class to get a Date object, it returns automatically to local time.
Code:
Calendar cal = Calendar.getInstance(TimeZone.getTimezone("GMT"));
cal.set(Calendar.HOUR_OF_DAY, 23);
System.out.System.out.println(cal.get(Calendar.HOUR_OF_DAY) );
/ / 25 well done
System.out.System.out.println(cal.getTime().getHours());
/ / gives 1 ...
I think there is a simple conversion of milliseconds regardless of anything else.
Reply With Quote
  #4  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Query with Date in java

Hello,
I think you are getting it wrong. Already method getHours () is deprecated and should not be used, then in its documentation it is stated that the reference time in the timezone of the machine and if you have the same timezone as me there are 2 hours apart with the GMT time zone. If you want more information on this then you can visit the official site of the sun for the api on java, that will definitely help you.
Reply With Quote
  #5  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 447
Re: Query with Date in java

Hello,
Just have a look at the following code, this is the basic code showing how to use the format of date in java.
Code:
DateFormat dtfr = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");
        java.util.Date dt = new java.util.Date ();
        String dtStr = dtfr.format (dt);
        try{
        Date dt2 = dtfr.parse (dtStr);
        }catch(ParseException pe){
            pe.printStackTrace();}
Reply With Quote
  #6  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Query with Date in java

Hello,
This is a advanced code, please go through it and understand as much as you can.
Code:
public boolean isAfterPayDay(DateTime dttm) {
  if (dttm.getMonthOfYear() == 2) {   // February is month 2!!
    return dttm.getDayOfMonth() > 26;
  }
  return dttm.getDayOfMonth() > 28;
}

public Days daysToNewYear(LocalDate fromDate) {
  LocalDate nwy = fromDate.plusYears(1).withDayOfYear(1);
  return Days.daysBetween(fromDate, nwy);
}

public boolean isRentalOverdue(DateTime dttmRented) {
  Period rntprd = new Period().withDays(2).withHours(12);
  return dttmRented.plus(rntprd).isBeforeNow();
}

public String getBirthMonthText(LocalDate dateOfBirth) {
  return dateOfBirth.monthOfYear().getAsText(Locale.ENGLISH);
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Query with Date in java"
Thread Thread Starter Forum Replies Last Post
Sql query for Date function. horuy Software Development 3 17-11-2010 12:22 AM
Query in java classes Aaliya Seth Software Development 5 26-02-2010 01:34 AM
Query regarding use of ActionListener in java beelow Software Development 3 16-11-2009 10:56 AM
[JAVA] query results in a combobox and jtextfield Aamin Software Development 5 31-03-2009 03:20 PM
Query Disabled Date Kurt Levitan Window 2000 Help 5 09-11-2007 08:20 PM


All times are GMT +5.5. The time now is 12:01 PM.