标签:
package cn.hncu.day9;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
public class PropertiesDemo {
//注意,a.properties文件应该放在项目根目录,即和src,bin文件夹并列的地方
public static void main(String[] args) {
//掌握Properties,只要会如下几个方法就玩转: Properties(), load(), store(), setProperty(),getProperty()
//1 Properties()
Properties p = new Properties();
//2 load()
try {
InputStream in = new FileInputStream("a.properties");
p.load(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//3 getProperty()
System.out.println( p.getProperty("size") );
System.out.println( p.getProperty("times") );
System.out.println( p.getProperty("name") );//property文件中的汉字需要转码,否则会乱码,而MyEclipse中的property文件编辑器自动会帮我们做的
//4 setProperty()
p.setProperty("size", "123");//注意,key和value都是文本
int v = Integer.parseInt(p.getProperty("times"))+123;
p.setProperty("times", ""+v);
//5 store()
try {
OutputStream out = new FileOutputStream("a.properties");
p.store(out, "湖南城院");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("-----------------");
//罗列出系统(Java虚拟机)的属性
Properties p2 = System.getProperties();
Enumeration e = p2.propertyNames();
while(e.hasMoreElements()){
String key = (String) e.nextElement();
String value = p2.getProperty(key);
System.out.println(key+"="+value);
}
}
}
-----------------------------------------------------------------
package cn.hncu.day9;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
public class AppTryTimes {
public static void main(String[] args) {
if( !countTimes() ){
JOptionPane.showMessageDialog(null, "软件试用次数已满,请注册!");
return;
}
System.out.println("程序正常运行....");
}
private static boolean countTimes() {
Properties p = new Properties();
int count = 0;
try {
File file = new File("times.uni");
if(!file.exists()){
file.createNewFile();
}
FileInputStream fin = new FileInputStream("times.uni");
p.load(fin);
String strValue = p.getProperty("count");
if(strValue!=null){
count = Integer.parseInt(strValue);
if(count>=5){
return false;
}
}
count++;
System.out.println("软件已经运行"+count+"次!");
p.setProperty("count", ""+count);
//new FileOutputStream(file)---这一句运行时会把原来的旧文件充掉
p.store(new FileOutputStream(file),"");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
------------------------------------------------------------------------
package cn.hncu.day9;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
//demo1();//计算程序耗时
demo2();
}
private static void demo2() {
//日期计算
Date d = new Date();//当前日期时间(对象)
System.out.println(d);
long t = 5*24*60*60*1000 + d.getTime();//5天以后的日期对象的精确值
d.setTime(t);
//格式化: 日期对象-->日期的字符串
//DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SS毫秒");
String str = df.format(d);
System.out.println("str:"+str);
//解析: 日期的字符串-->日期对象
DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date d2 = df2.parse("08/08/2008 08:08:08");
System.out.println(d2);
System.out.println( df.format(d2) );
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void demo1(){
Date d = new Date();//对象,内部存储的精确值:1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数
long t = d.getTime();//获取其中的精确值
for(int i=0;i<1000;i++){
System.out.println(i);
}
Date d2 = new Date();
long t2 = d2.getTime();
System.out.println("本程序的运行时间:"+ (t2-t));
}
}
-------------------------------------------------------------------------------------------------------------
package cn.hncu.day9;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println( c.get(Calendar.YEAR) );//获取年份
System.out.println( c.get(Calendar.MONTH) +1 );//注意,月份要加1
System.out.println( c.get(Calendar.DAY_OF_MONTH) );//获取日期
//显示星期几
//System.out.println( c.get(Calendar.DAY_OF_WEEK)-1 );//我们的星期一是它的第2天,因为它的第1天是从星期日开始的
System.out.println( getWeekDay(c) );
//日期计算
c.set(Calendar.YEAR, 3000);//给日历中的某个字段直接设置值
System.out.println( getWeekDay(c) );
//System.out.println(new Date().getTime());
c.setTime( new Date(1465215452531L+5*24*60*60*1000) );
System.out.println( c.get(Calendar.DAY_OF_MONTH) );//获取日期
System.out.println( getWeekDay(c) );
Calendar c2 = Calendar.getInstance();
//c2.add(Calendar.DAY_OF_MONTH,100);//100天后的日期(日历)对象
c2.add(Calendar.DAY_OF_MONTH,-100);//100天以前的日期(日历)对象
System.out.println( c2.get(Calendar.MONTH)+1 );
System.out.println( getWeekDay(c2) );
}
/*自己写的一个正确显示星期几的小工具*/
public static String getWeekDay(Calendar c){
String strs[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return strs[ c.get(Calendar.DAY_OF_WEEK)-1 ];
}
}
Properties文件,Data,Calendar类的使用
标签:
原文地址:http://www.cnblogs.com/1314wamm/p/5600101.html