`
robinfung
  • 浏览: 52670 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

myJava

阅读更多
	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;
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics