1. java web 開發如何控制使用期限及綁定伺服器mac
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
/**
* 與系統相關的一些常用工具方法.
*
* @author lvbogun
* @version 1.0.0
*/
public class SystemTool {
/**
* 獲取當前操作系統名稱. return 操作系統名稱 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}
/**
* 獲取unix網卡的mac地址. 非windows的系統默認調用本方法獲取.
* 如果有特殊系統請繼續擴充新的取mac地址方法.
*
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
// linux下的命令,一般取eth0作為本地主網卡
process = Runtime.getRuntime().exec("ifconfig eth0");
// 顯示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
// 尋找標示字元串[hwaddr]
index = line.toLowerCase().indexOf("hwaddr");
if (index >= 0) {// 找到了
// 取出mac地址並去除2邊空格
mac = line.substring(index + "hwaddr".length() + 1).trim();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* 獲取widnows網卡的mac地址.
*
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
// windows下的命令,顯示信息中包含有mac地址信息
process = Runtime.getRuntime().exec("ipconfig /all");
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
// 尋找標示字元串[physical
index = line.toLowerCase().indexOf("physical address");
if (index >= 0) {// 找到了
index = line.indexOf(":");// 尋找":"的位置
if (index >= 0) {
System.out.println(mac);
// 取出mac地址並去除2邊空格
mac = line.substring(index + 1).trim();
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* windows 7 專用 獲取MAC地址
*
* @return
* @throws Exception
*/
public static String getMACAddress() throws Exception {
// 獲取本地IP對象
InetAddress ia = InetAddress.getLocalHost();
// 獲得網路介面對象(即網卡),並得到mac地址,mac地址存在於一個byte數組中。
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
// 下面代碼是把mac地址拼裝成String
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是為了把byte轉化為正整數
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
// 把字元串所有小寫字母改為大寫成為正規的mac地址並返回
return sb.toString().toUpperCase();
}
}
寫一個全局攔截的servlet,只要有請求的時候就調用這個類裡面的獲取mac地址的方法
String os = getOSName();
System.out.println(os);
if (os.equals("windows 7")) {
String mac = getMACAddress();
System.out.println(mac);
} else if (os.startsWith("windows")) {
// 本地是windows
String mac = getWindowsMACAddress();
System.out.println(mac);
} else {
// 本地是非windows系統 一般就是unix
String mac = getUnixMACAddress();
System.out.println(mac);
}
記得判斷一下是什麼系統
2. JAVA裡面怎麼用一個時間減去一個時間段(例如5天),得到另外一個時間最好有源程序啊
用這個類Calendar
例如:
Calendar c = new GregorianCalendar();
c.setTime(new Date());//設置時間
c.add(Calendar.DATE, -1);//這句就是所設置的時間減去一天,第二個參數為正是加,為負就是減
c.getTime();//就是最後得到的時間
不知道解決你的問題沒.
3. 怎麼實現java:自動編號規則:日期+當天項目的編號 把這個編號要存到資料庫
日期直接用日期格式化就行。
當天項目編號簡單點的話可以存放到資料庫(如mysql)中,下一天0點清零就行。更好的做法是存放到redis中,也是下一天清零。
4. 求教用java編寫一個程序要求給定一個日期值,計算若干天後的日期值,和給定兩個日期計算它們之間相距的天
||public static int getIntervalDays(String date1, String date2) {
return getIntervalDays(getTime(date1), getTime(date2));
}
public static int getIntervalDays(long s1, long s2) {
return (int) ( (s1 - s2) / (24 * 3600 * 1000L));
}
public static java.sql.Date str2date(String sDate) {
if (sDate == null || sDate.equals(""))
return null;
if (sDate.charAt(0) > '9' || sDate.charAt(0) < '0')
return null;
sDate = sDate.replace('/', '-');
String[] ar = strSplit(sDate, "- :");
if (ar.length < 3)
return null;
return java.sql.Date.valueOf(ar[0] + "-" + ar[1] + "-" + ar[2]);
}
public static long getTime(Object date) {
if (isEmpty(date))
return System.currentTimeMillis();
return str2date(date.toString()).getTime();
}
public static String getDateFromDate(String date, int avail) {
long lg = getTime(date);
return getDateString(lg + (long) avail * 86400000L);
}
public static String getDateString(long mill) {
java.sql.Date date = new java.sql.Date(mill);
return date.toString();
}
5. java編寫程序,當以年-月-日的格式輸入一個日期時,輸出其該年是否為閏年,該月有幾天, 該日是星期幾
//1.將字元串用split切割得到年月日組成的數組 String s2="2011-11-11 11:11:11";
//2.Calendar對象的獲得,abstract並且構造函數是protected
//本地時區和本地的習慣,系統日期
Calendar calendar=Calendar.getInstance();
//3.將Calendar轉換成輸入的日期
用calendar對象的set(Calendar.對應常量(如YEAR等),輸入的對應值)方法設值
//4.獲得判斷用的值
//獲得年份
int year=calendar.get(Calendar.YEAR);
//獲得這個月最多的天數
int maxDay=today.getActualMaximum(Calendar.DATE);
//獲得當前日期是一周中的第幾天,注意這個數不代表星期幾而是你電腦上日歷的第幾列
int weekDay=calendar.get(Calendar.DAY_OF_WEEK);
}
好了就這么多吧,有這些差不多了,還有什麼繼續問哈
6. 你好,我想問一下怎麼用Java代碼限制軟體使用期限,能給詳細代碼么
獲取系統的當前時間,然後減去注冊時間 如果超過了你設定的期限時間就是過期了
7. JAVA編寫的軟體,求人修改一下使用時間期限!
做的不好,請樓主和各位多提意見!!
工具: eclipse
文件名: Editor.java
代碼:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.StringTokenizer;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class Editor extends JFrame {
private JPanel jContentPane = null;
private JMenuBar jMenuBar = null;
private JMenu jMenu = null;
private JMenuItem jMenuItem = null;
private JMenuItem jMenuItem1 = null;
private JMenu jMenu1 = null;
private JMenuItem jMenuItem2 = null;
private JMenuItem jMenuItem3 = null;
private JTextArea jTextArea = null;
private JScrollPane jScrollPane = null;
private File openFile=null;
/**
* This is the default constructor
*/
public Editor() {
super();
initialize();
}
/**
* This method initializes this
*
* @ void
*/
private void initialize() {
this.setSize(300, 200);
this.setJMenuBar(getJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
public JMenuBar getJMenuBar() {
if (jMenuBar == null) {
jMenuBar = new JMenuBar();
jMenuBar.add(getJMenu());
jMenuBar.add(getJMenu1());
}
return jMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
jMenu.setText("file");
jMenu.setBounds(new java.awt.Rectangle(0,0,35,35));
jMenu.add(getJMenuItem());
jMenu.add(getJMenuItem1());
}
return jMenu;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem() {
if (jMenuItem == null) {
jMenuItem = new JMenuItem();
jMenuItem.setText("open");
jMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
openFile=new File(JOptionPane.showInputDialog(null,"請輸入有效的文件路徑","文件選擇",JOptionPane.INFORMATION_MESSAGE));
FileInputStream fileIS = null;
BufferedInputStream filePS=null;
byte[] fileCode;
if(openFile.isFile()){
try {
fileIS=new FileInputStream(openFile);
filePS=new BufferedInputStream(fileIS);
fileCode = new byte[fileIS.available()];
filePS.read(fileCode);
jTextArea.setText(new String(fileCode));
filePS.close();
fileIS.close();
jMenuItem1.setEnabled(true);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
} else{
JOptionPane.showMessageDialog(null,"打開文件路徑錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
}
}
});
}
return jMenuItem;
}
/**
* This method initializes jMenuItem1
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem1() {
if (jMenuItem1 == null) {
jMenuItem1 = new JMenuItem();
jMenuItem1.setText("save");
jMenuItem1.setEnabled(false);
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
PrintStream filePS = null;
File saveFile=null;
String fileName = JOptionPane.showInputDialog("輸入保存文件的路徑(默認為原路徑)");
if(fileName.equals("") || fileName==null)saveFile = openFile;
else saveFile = new File(fileName);
if(saveFile.isFile()){
try {
filePS=new PrintStream(new FileOutputStream(openFile));
filePS.print(jTextArea.getText());
filePS.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} finally{
filePS.close();
}
} else {
JOptionPane.showMessageDialog(null,"保存文件路徑錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
}
}
});
}
return jMenuItem1;
}
/**
* This method initializes jMenu1
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu1() {
if (jMenu1 == null) {
jMenu1 = new JMenu();
jMenu1.setText("util");
jMenu1.add(getJMenuItem2());
jMenu1.add(getJMenuItem3());
}
return jMenu1;
}
/**
* This method initializes jMenuItem2
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem2() {
if (jMenuItem2 == null) {
jMenuItem2 = new JMenuItem();
jMenuItem2.setText("count");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JOptionPane.showMessageDialog(null,"當前文件的字元數:"+jTextArea.getText().length());
}
});
}
return jMenuItem2;
}
/**
* This method initializes jMenuItem3
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem3() {
if (jMenuItem3 == null) {
jMenuItem3 = new JMenuItem();
jMenuItem3.setText("reverse");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
final StringTokenizer textTokenizer = new StringTokenizer(jTextArea.getText(),"\n");
final StringBuffer textBuffer = new StringBuffer(jTextArea.getText());
Timer replaceTimer = new Timer(100, new ActionListener() {
StringBuffer replaceBuffer = null;
int start = 0;
int end = 0;
public void actionPerformed(ActionEvent e) {
if(textTokenizer.hasMoreTokens()){
replaceBuffer = new StringBuffer(textTokenizer.nextToken());
end = textBuffer.indexOf("\n",end+1);
if(end<0) end = textBuffer.length();
start = end - replaceBuffer.length();
textBuffer.replace(start,end,replaceBuffer.reverse().toString());
jTextArea.setText(textBuffer.toString());
}
}
});
replaceTimer.start();
}
});
}
return jMenuItem3;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
}
return jTextArea;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}
public static void main(String args[]){
Editor editor = new Editor();
editor.setVisible(true);
}
}
說實話自己覺得做的並不好,不過事件都有了!
8. 如何給javaweb程序添加使用期限製程序
下一個過濾器,設定一個過期時間就好了,到期filter直接返回不進入action
9. 我用java編一個圖書管理程序,借書日期是自動添加當前日期時間,可是應還日期是一個月後,怎麼寫
1.先用jdbc連接資料庫復,這制個網上好多你去搜下就有
2.借書還書就是對某張表的更新操作,你借書還書的時候可以得到該書的一個id,這時,你通過這個id從資料庫中找到所對應的數據,更改書得庫存量即可。
自己動手做一遍,其實不難的,加油!
10. JAVA程序設計:設定一個日期類,成員變數有年、月、日定義三個成員方法,方法要求在下面:
packageimage;
publicclassLDate
{
Stringyear;
Stringmonth;
Stringday;
privatevoidsetDate(Stringyear,Stringmonth,Stringday)
{
.year=year;
this.month=month;
this.day=day;
}
privatebooleanisLeapYear(Stringy)
{
intyear=Integer.parseInt(y);
return(year%400==0||year%100!=0&&year%4==0);
}
privatevoidoutputDate()
{
StringBuilderbuilder=newStringBuilder();
builder.append("LDate[year=");
builder.append(year);
builder.append(",month=");
builder.append(month);
builder.append(",day=");
builder.append(day);
builder.append("]");
System.out.println(builder.toString());
}
publicstaticvoidmain(String[]args)
{
LDateld=newLDate();
ld.setDate("2014","01","11");
System.out.println(ld.isLeapYear("2014"));
ld.outputDate();
}
}