Results 1 to 6 of 6

Thread: Query with Date in java

  1. #1
    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));

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    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();}

  6. #6
    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);
    }

Similar Threads

  1. Sql query for Date function.
    By horuy in forum Software Development
    Replies: 3
    Last Post: 17-11-2010, 12:22 AM
  2. Query in java classes
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 26-02-2010, 01:34 AM
  3. Query regarding use of ActionListener in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 10:56 AM
  4. [JAVA] query results in a combobox and jtextfield
    By Aamin in forum Software Development
    Replies: 5
    Last Post: 31-03-2009, 02:20 PM
  5. Windows 2000 Server query to disable date
    By Saphire in forum Window 2000 Help
    Replies: 2
    Last Post: 09-11-2007, 08:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,582,029.38185 seconds with 17 queries