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