public long getSleepTime(Properties props){ long time = 0; String key = null; Object obj = null; String value = null; String[] values = null; Iterator it = props.keySet().iterator(); while(it.hasNext()) { key = (String) it.next(); if (KEY_SLEEP_TIME.equals(key)) { value = props.getProperty(key); time = Long.parseLong(value); } } return time; }
File
BufferedReader Fin = new BufferedReader(new InputStreamReader(new FileInputStream(ff), PruDefinition.ENC_BIG5)); PrintWriter fout = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outFilename), PruDefinition.ENC_BIG5_HK));
Duplicate
PrintWriter fout = new PrintWriter(new BufferedWriter(new FileWriter(outFilename))); BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(f), ENC_BIG5));
Manipulating date in Java, solve the century bug
On and off we need to modify/create date. For example, we need to search data using date. To do that, we need to create Date object. Even though we can create Date object using the constructor as illustrated below.
Date date = new Date(2010, 1, 1);
However, this method is deprecated and we should not use it. Instead, use the Calendar object as shown below.
Calendar calendar = Calendar.getInstance(); calendar.set(2010, 1, 1); Date date = calendar.getTime(); int hours = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); int seconds = calendar.get(Calendar.SECOND); int millisecond = calendar.get(Calendar.MILLISECOND); System.out.println(seconds); System.out.println(millisecond);
After JDK1.5 looping Hashtable is more easy
Hashtable ht = new Hashtable(); ht.put("a", new Integer(1)); ht.put("b", new Integer(2)); ht.put("c", new Integer(3)); Enumeration<String> e = ht.keys(); while (e.hasMoreElements()) { String key = e.nextElement(); System.out.println(key.toString() + "=" + ht.get(key)); }
DateUtil
private static final Map<String, String> DATE_FORMAT_REGEXPS = new HashMap<String, String>() {{ put("^\\d{8}$", "yyyyMMdd"); put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy"); put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd"); put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy"); put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd"); put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}$", "dd MMM yyyy"); put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}$", "dd MMMM yyyy"); put("^\\d{12}$", "yyyyMMddHHmm"); put("^\\d{8}\\s\\d{4}$", "yyyyMMdd HHmm"); put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}$", "dd-MM-yyyy HH:mm"); put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy-MM-dd HH:mm"); put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}$", "MM/dd/yyyy HH:mm"); put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy/MM/dd HH:mm"); put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMM yyyy HH:mm"); put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMMM yyyy HH:mm"); put("^\\d{14}$", "yyyyMMddHHmmss"); put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss"); put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd-MM-yyyy HH:mm:ss"); put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss"); put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "MM/dd/yyyy HH:mm:ss"); put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy/MM/dd HH:mm:ss"); put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMM yyyy HH:mm:ss"); put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMMM yyyy HH:mm:ss"); put("\\d{1,2}/\\d{1,2}/\\d{2}\\s\\d{1,2}:\\d{1,2}:\\d{1,2}:\\d{1,3}", "dd/MM/yyyy HH:mm:ss:SSS"); }};
Get Classpath .properties
public Properties initProperty(String path) throws FileNotFoundException, IOException{ Properties props = new Properties(); InputStream is = null; is = this.getClass().getClassLoader().getResourceAsStream(path); props.load(is); return props; }
Looping to get the value from .properties
public long getSleepTime(Properties props){ long time = 0; String key = null; Object obj = null; String value = null; String[] values = null; Iterator it = props.keySet().iterator(); while(it.hasNext()) { key = (String) it.next(); if (KEY_SLEEP_TIME.equals(key)) { value = props.getProperty(key); time = Long.parseLong(value); } } return time; }
count working hours
public static int countWorkingHours(Date startDate, Date endDate) { List<Integer> workingHours = new ArrayList<Integer>() { { add(9); add(10); add(11); add(12); // add(13); // add(14); add(15); add(16); add(17); add(18); } }; Calendar startCal; Calendar endCal; startCal = Calendar.getInstance(); startCal.setTime(startDate); endCal = Calendar.getInstance(); endCal.setTime(endDate); int workHours = 0; // Return 0 if start and end are the same if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) { return 0; } if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) { startCal.setTime(endDate); endCal.setTime(startDate); } do { startCal.add(Calendar.HOUR_OF_DAY, 1); if (workingHours.contains(startCal.get(Calendar.HOUR_OF_DAY))) { ++workHours; } } while (startCal.getTimeInMillis() < endCal.getTimeInMillis()); return workHours; }
相关推荐
Android开启、关闭WiFi热点 亲测Android6.0和Android10.0
MyJAVA(tarena课堂笔记总结)集合框架,内部类,反射,线程,I/O流
myjava.js
。。。
用struts+hibernate实现案例
Myjava的
开发的一个用java语言实现的编译器,内含词法分析器,语法分析器,而且可以实现中间代码生成.用到了SLR算法和LR算法
。。。
编译原理,LR(0)程序,可以分析串是否出自该文法
java版象棋,可以实现简单的下象棋,呵呵挺简单的
java程序,综合大作业。个人简历制作。java编程
一个简单的UNCODE 码生成程序,功能很简单
该系统在磁盘中存储全校的教室信息,例如: 全校教室总数,每个教室可容纳的人数,各教室所在的楼号、教室号;每幢教学楼的教室数,各教室可容纳的人数,各教室的设置(教室分为:普通教室、阶梯教室、语音教室、...
myJava 使用java编写的一些有意思的小程序 关于 这里面是我整理的一些运用java编写的一些有趣的代码,每一份java文件都是一个有趣的小程序 使用 主要代码就在src目录下,每一份java文件都是独立的 1、可直接使用cmd...
MyJava:java学习心得
git_java git myjava
MyJava的 我的Java实践
java系统原始码Myjava的 我的java源代码管理系统
MyJava的