📄 poptoolkit.java
字号:
/* * @(#) PopToolkit.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.misc;//导入核心Java类库import java.io.File;import java.io.Writer;import java.io.FileWriter;import java.io.IOException;import java.awt.Window;import java.awt.Toolkit;import java.awt.Dimension;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Calendar;//导入第三方Java类库import org.apache.commons.codec.binary.Base64;/** * 常用工具箱 * * @version 0.1 2005-08-29 * @author Hwerz */public class PopToolkit extends Object { /*------------------------------------------------------------------------* * 属性定义 * *------------------------------------------------------------------------*/ /** * 静态常量 */ private static final String ZERO = "0"; /** * Base64工具类 */ private static final Base64 BASE64 = new Base64(); /*------------------------------------------------------------------------* * 构造函数 * *------------------------------------------------------------------------*/ /** * 构造函数为私有,这样在整个运行过程中该类就只能有一个实例 */ private PopToolkit() { super(); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 返回当天的日期 * * @return 当天的日期 */ public static String getCurrentDate() { StringBuffer buffer = new StringBuffer(); Calendar calendar = Calendar.getInstance(); buffer.append(calendar.get(Calendar.YEAR)); buffer.append("-"); //注意:月份从0算起 int month = calendar.get(Calendar.MONTH) + 1; if (month < 10) { buffer.append(ZERO); } buffer.append(month); buffer.append("-"); if (calendar.get(Calendar.DAY_OF_MONTH) < 10) { buffer.append(ZERO); } buffer.append(calendar.get(Calendar.DAY_OF_MONTH)); return buffer.toString(); } /** * 返回当时的时间 * * @return 当时的时间 */ public static String getCurrentTime() { StringBuffer buffer = new StringBuffer(); Calendar calendar = Calendar.getInstance(); buffer.append(calendar.get(Calendar.HOUR_OF_DAY)); buffer.append(":"); int minute = calendar.get(Calendar.MINUTE); if (minute < 10) { buffer.append(ZERO); } buffer.append(minute); buffer.append(":"); int second = calendar.get(Calendar.SECOND); if (second < 10) { buffer.append(ZERO); } buffer.append(second); return buffer.toString(); } /** * 返回当时时间的长格式形式 * * @return 当时时间的长格式形式 */ public static String getLongTime() { StringBuffer time = new StringBuffer(); time.append(getCurrentDate()); time.append(" "); time.append(getCurrentTime()); return time.toString(); } /** * 将指定的字符串保存到指定的文件中 * * @param string 指定的字符串 * @param file 指定的文件 */ public static void saveString2File(String string, File file) { try { Writer writer = new FileWriter(file); writer.write(string); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 判断指定的年份是否是闰年 * * @param year 指定的年份 * @return 如果指定的年份是闰年则返回true,否则返回false */ public static boolean isLeapYear(int year) { boolean leap = false; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { leap = true; } return leap; } /** * 返回本地IP地址 * * @return 本地IP地址 */ public static String getLocalIP() { String ip = "UnknownHost"; try { ip = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } return ip; } /** * 检查指定的文件名是否合法 * * @param name 指定的文件名 * @return 如果指定的文件名合法则返回true,否则返回false */ public static boolean checkFileName(String name) { boolean valid = true; if (name == null) { valid = false; } else if (name.length() == 0) { valid = false; } else if (name.length() == 1 && name.equals(".")) { valid = false; } else { char[] chars = {'\\', '/', ':', '*', '?', '<', '>', '|', '\"'}; for (int i = 0; i < name.length(); i++) { char c = name.charAt(i); for (int j = 0; j < chars.length; j++) { if (c == chars[j]) { valid = false; break; } } if (valid == false) { break; } } } return valid; } /** * 检查指定的字符串是否可以转化为合法的整数 * * @param value 指定的字符串 * @return 如果指定的字符串可以转化为合法的整数则返回true,否则返回false */ public static boolean checkNumber(String value) { boolean valid = true; if (value == null) { valid = false; } else if (value.length() == 0) { valid = false; } else { char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); for (int j = 0; j < chars.length; j++) { valid = false; if (c == chars[j]) { valid = true; break; } } if (valid == false) { break; } } } return valid; } /** * 将指定的窗体对象置于屏幕中间 * * @param window 待置于屏幕中间的窗体对象 */ public static void makeWindowCenter(Window window) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension windowSize = window.getSize(); if (windowSize.height > screenSize.height) { windowSize.height = screenSize.height; } if (windowSize.width > screenSize.width) { windowSize.width = screenSize.width; } window.setLocation((screenSize.width - windowSize.width) / 2, (screenSize.height - windowSize.height) / 2); window.setVisible(true); } /** * 采用Base64算法编码 * * @param data 待编码的数据 * @return 编码后的数据 */ public static String encode(byte[] data) { return new String(BASE64.encode(data)); } /** * 采用Base64算法解码 * * @param data 待解码的数据 * @return 解码后的数据 */ public static byte[] decode(String data) { return BASE64.decode(data.getBytes()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -