2012-12-05 29 views
1

我想從Google或YouTube或互聯網上的任何地方獲取當前的日期和時間。我不想得到設備或系統的時間。如何使用互聯網而不是設備或系統獲取當前日期和時間?

+1

這你可能會感興趣:http://stackoverflow.com/questions/7677674/use-of-ntp - 服務 – lfk

+0

請記住,如果您嘗試使用互聯網時間,您應該考慮至少有一個或**所有**網站,您打算用於獲取日期/時間將不** **可訪問運行。 –

+0

在我的項目中,我在登錄時需要它。它僅用於同步。所以如果有什麼辦法可以從互聯網上獲得時間,那麼請告訴我。我也會檢查互聯網連接。 –

回答

1

這是我做的(請注意,您將需要下載並導入apache libraries):

package timeinternet; 
import java.net.InetAddress; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.TimeZone; 
import org.apache.commons.net.ntp.NTPUDPClient; 
import org.apache.commons.net.ntp.TimeInfo; 

/** 
* 
* @author Windows 8 Java 
*/ 
public class NewJFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jLabel1 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel1.setText("jLabel1"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(206, Short.MAX_VALUE) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(40, 40, 40)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(141, 141, 141) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(58, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) throws Exception { 
     /* 
     * Set the Nimbus look and feel 
     */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* 
     * If Nimbus (introduced in Java SE 6) is not available, stay with the 
     * default look and feel. For details see 
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     String str; 
     String TIME_SERVER = "time-a.nist.gov"; 
NTPUDPClient timeClient = new NTPUDPClient(); 
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER); 
TimeInfo timeInfo = timeClient.getTime(inetAddress); 
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); 
//long returnTime = timeInfo.getMessage().getReceiveTimeStamp().getTime(); 
Date time = new Date(returnTime); 
System.out.println("" + time); 


Calendar calendar = Calendar.getInstance(); 
     TimeZone fromTimeZone = calendar.getTimeZone(); 
     TimeZone toTimeZone = TimeZone.getTimeZone("CST"); 

     calendar.setTimeZone(fromTimeZone); 
     calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1); 
     if (fromTimeZone.inDaylightTime(calendar.getTime())) { 
      calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1); 
     } 

     calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset()); 
     if (toTimeZone.inDaylightTime(calendar.getTime())) { 
      calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings()); 
     } 

     System.out.println(calendar.getTime()); 




str = (time.toString()); 
//jLabel1.setText("a"); 
     /* 
     * Create and display the form 
     */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new NewJFrame().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    // End of variables declaration     
} 
相關問題