Java/J2ME/Date

Материал из Java эксперт
Перейти к: навигация, поиск

Date Field

   <source lang="java">

/*--------------------------------------------------

  • Snooze.java
  • Example from the book: Core J2ME Technology
  • Copyright John W. Muchow http://www.CoreJ2ME.ru
  • You may use/modify for any non-commercial purpose
  • -------------------------------------------------*/

import java.util.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.Timer; import java.util.TimerTask; public class Snooze extends MIDlet implements ItemStateListener, CommandListener {

 private Display display;          // Reference to display object 
 private Form fmMain;              // The main form
 private Command cmSnooze;         // Start the timer
 private Command cmReset;          // Reset to current date/time
 private Command cmExit;           // Exit the MIDlet
 private DateField dfSnoozeTime;   // How long to snooze
 private int dateIndex;            // Index of the DateField on the Form
 private Date currentTime;         // Current time...changes when pressing reset
 private Timer tmSnooze;           // The timer - keeps track of system time
 private SnoozeTimer ttSnooze;     // Called by the timer
 private boolean dateOK = false;   // Was the user input valid?
 
 public Snooze()
 {
   display = Display.getDisplay(this);
   // The main form
   fmMain = new Form("When to sound the alarm:");
   // Save today"s date
   currentTime = new Date();
   
   // DateField with todays date as a default
   dfSnoozeTime = new DateField("", DateField.DATE_TIME);
   dfSnoozeTime.setDate(currentTime);
   // All the commands/buttons
   cmSnooze = new Command("Snooze", Command.SCREEN, 1);
   cmReset = new Command("Reset", Command.SCREEN, 1);
   cmExit = new Command("Exit", Command.EXIT, 1);    
   
   // Add to form and listen for events
   dateIndex = fmMain.append(dfSnoozeTime);
   fmMain.addCommand(cmSnooze);
   fmMain.addCommand(cmReset);    
   fmMain.addCommand(cmExit);    
   fmMain.setCommandListener(this);
   fmMain.setItemStateListener(this);    
 }
 public void startApp ()
 {
   display.setCurrent(fmMain);
 }
 public void pauseApp()
 { }
 
 public void destroyApp(boolean unconditional)
 { }
 public void itemStateChanged(Item item)              
 {
   if (item == dfSnoozeTime)
   {
     // If the user selected date and/or time that is earlier 
     // than today, set a flag. We are using getTime() 
     // method of the Date  class, which returns the # of 
     // milliseconds since January 1, 1970
     if (dfSnoozeTime.getDate().getTime() < currentTime.getTime())
       dateOK = false;
     else
       dateOK = true;
   }        
 }
 
 public void commandAction(Command c, Displayable s)
 {
   if (c == cmSnooze)
   {
     if (dateOK == false)
     {
       Alert al = new Alert("Unable to set alarm", "Please choose another date and time.", null, null);
       al.setTimeout(Alert.FOREVER);
       al.setType(AlertType.ERROR);
       display.setCurrent(al);      
     }
     else
     {
       // Create a new timer
       tmSnooze = new Timer();
       ttSnooze = new SnoozeTimer();
       // Amount of time to delay
       long amount = dfSnoozeTime.getDate().getTime() - currentTime.getTime();
       tmSnooze.schedule(ttSnooze,amount);          
       // Remove the commands
       fmMain.removeCommand(cmSnooze);
       fmMain.removeCommand(cmReset);    
       
       // Remove the DateField
       fmMain.delete(dateIndex);
       
       // Change the Form message
       fmMain.setTitle("Snoozing...");
     }
   }
   else if (c == cmReset)
   {
     // Reset to the current date/time
     dfSnoozeTime.setDate(currentTime = new Date());
   }
   else if (c == cmExit)
   {
     destroyApp(false);
     notifyDestroyed();
   }
 }
 // Handle the timer task
 private class SnoozeTimer extends TimerTask
 {
   public final void run()
   {
     Alert al = new Alert("Time to wake up!");
     al.setTimeout(Alert.FOREVER);
     al.setType(AlertType.ALARM);
     AlertType.ERROR.playSound(display);
     display.setCurrent(al);       
     
     // Cancel this timer task
     cancel(); 
   }
 }

}


      </source>
   
  
 
  



Date Today

   <source lang="java">

//jad file (please verify the jar size) /* MIDlet-Name: DateToday MIDlet-Version: 1.0 MIDlet-Vendor: MyCompany MIDlet-Jar-URL: DateToday.jar MIDlet-1: DateToday, , DateToday MicroEdition-Configuration: CLDC-1.0 MicroEdition-Profile: MIDP-1.0 MIDlet-JAR-SIZE: 100

  • /

import java.util.Date; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.DateField; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.midlet.MIDlet; public class DateToday extends MIDlet implements CommandListener {

 private Display display;
 private Form form = new Form("Today"s Date");
 private Date today = new Date(System.currentTimeMillis());
 private Command exit = new Command("Exit", Command.EXIT, 1);
 private DateField datefield = new DateField("", DateField.DATE_TIME);
 public DateToday() {
   display = Display.getDisplay(this);
   datefield.setDate(today);
   form.append(datefield);
   form.addCommand(exit);
   form.setCommandListener(this);
 }
 public void startApp() {
   display.setCurrent(form);
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
 }
 public void commandAction(Command command, Displayable displayable) {
   if (command == exit) {
     destroyApp(false);
     notifyDestroyed();
   }
 }

}

      </source>
   
  
 
  



KVM Calendar

   <source lang="java">

import java.util.Calendar; import java.util.Date; public class KVMCalendar {

 public static void main(String[] args) {
   Calendar cal = Calendar.getInstance();
   Date date = new Date();
   cal.setTime(date);
   int month = cal.get(Calendar.MONTH);
   int day = cal.get(Calendar.DAY_OF_MONTH);
   System.out.println("Day is " + day + ", month is " + month);
   final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;
   long offset = date.getTime();
   offset += 20 * MILLIS_PER_DAY;
   date.setTime(offset);
   cal.setTime(date);
   month = cal.get(Calendar.MONTH);
   day = cal.get(Calendar.DAY_OF_MONTH);
   System.out.println("In 20 days time, day will " + day + ", month will be " + month);
   System.out.println(cal);
 }

}


      </source>
   
  
 
  



KVM Time Zones

   <source lang="java">

import java.util.TimeZone; public class KVMTimeZones {

   public static void main(String[] args) {
       TimeZone defaultTZ = TimeZone.getDefault();
       String[] ids = TimeZone.getAvailableIDs();
       
       System.out.println("Available time zone ids: ");
       for (int i = 0; i < ids.length; i++) {
           System.out.print(ids[i] + " ");
       }
       System.out.println("\nDefault timezone is " + defaultTZ.getID() + 
                       ", GMT offset = " + defaultTZ.getRawOffset());
   }

}


      </source>