--------------------ASP.Net+Android+IOS开发、.Net培训、期待与您交流! --------------------
1. System
下面介绍一个方法,获得系统运行的一些文件信息
import java.util.Properties;
public class ClassDemo {
public static void main(String[] args) {
/* static Properties getProperties()获得系统的所有属性
* Properties是Hashtable的子类,所以我们可以使用keySet获得键集,然后获得信息 */
Properties p = System.getProperties();
/*static String getProperty(String key)方法获得指定键的值*/
System.out.println("user.country:"+p.getProperty("user.country"));
/*static String setProperty(Stringkey,String value)
* 利用此方法可以自定义设置属性值*/
p.setProperty("mySet", "helloworld");
System.out.println("mySet:"+p.getProperty("mySet"));
System.out.println("下面是系统的属性,开始读取:");
for(Object ob:p.keySet()){
System.out.println(ob+":"+p.getProperty((String)ob));
}
}
}
部分结果:
user.country:CN
mySet:hello world
下面是系统的属性,开始读取:
java.runtime.name:Java(TM)SE Runtime Environment
mySet:hello world
sun.boot.library.path:E:\instalfile\JDK1\bin
java.vm.version:23.7-b01
java.vm.vendor:OracleCorporation
java.vendor.url:http://java.oracle.com/
2. Runtime
import java.io.IOException;
import java.util.Properties;
public class ClassDemo {
public static void main(String[] args) throws IOException {
/*
* getRuntime()方法获得Runtime对象 Process exec(Stringcommand)开始执行程序
*/
Runtime r = Runtime.getRuntime();// 获得应用程序实例
Process qq = r.exec("D:\\QQ\\Bin\\QQ.exe");// 运行此程序
/* destroy()方法停止线程 */
try {
Thread.sleep(500);
qq.destroy();
} catch (InterruptedException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
}
public class Sy {
public static void main(String [] s) throws IOException,InterruptedException{
Runtime run=Runtime.getRuntime();
Process pro=run.exec("notepad.exe F:\\11.txt");//执行进程
Thread.sleep(5000);
pro.destroy();//结束进程
}
}
3. 日期类
1. Date,SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
public class ClassDemo {
public static void main(String[] args) {
Date d = new Date();
System.out.println("系统时间:" + d);
SimpleDateFormat sim = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
String newDate = sim.format(d);// 把系统时间按照自定义的格式输出
System.out.println("系统时间:" + newDate);
}
}
结果:
系统时间:Sun Sep 29 15:23:50 CST 2013
系统时间:2013年09月29日 03时23分50秒
2. Calendar
1.基本操作
package www.fuxi.IO;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class ClassDemo {
public static void main(String[] args) {
getYMD();// 获得年月日
SetDate();// 设置年月日
OpDate();
}
/* 获得单个的年月日 */
public static void getYMD() {
String[] months = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月",
"九月", "十月", "十一月", "十二月" };
Calendar calen = Calendar.getInstance();// 获得日历对象,也可以实例化子类
/* 获得年使用日历对象调用get()方法 */
System.out.println(calen.get(Calendar.YEAR));
/* 获得月使用日历对象调用get()方法 */
int index = Calendar.MONDAY;
System.out.println(months[calen.get(index)]);
/* 获得日使用日历对象调用get()方法 */
System.out.println(calen.get(Calendar.DAY_OF_MONTH));
}
/* 设置日期 */
private static void SetDate() {
/* final void set(int year, intmonth,int date)设置年月日 */
Calendar calen = Calendar.getInstance();
calen.set(2015, 04, 05);
System.out.print("设置到日期:");
System.out.println(calen.get(Calendar.YEAR) + "年"
+ calen.get(Calendar.MONTH) + "月"
+ calen.get(Calendar.DAY_OF_MONTH) + "日");
}
/* 设置日期,对日期字段进行操作 */
public static void OpDate() {
/* add(int field, int amount)对日期字段进行操作 */
Calendar calen = Calendar.getInstance();
System.out.println("当前日期:"+calen.get(Calendar.YEAR) + "年"
+ calen.get(Calendar.MONTH) + "月"
+ calen.get(Calendar.DAY_OF_MONTH) + "日");
calen.add(Calendar.YEAR, 10);// 将年增加20
System.out.println("增加10年:"+calen.get(Calendar.YEAR) + "年");
}
}
结果:
2013
九月
29
设置到日期:2015年4月5日
当前日期:2013年8月29日
增加10年:2023年
2.练习
获取某年的二月的天数
思路:先设置年,然后月和日为02,01,这据表示3月1号
然后将日减1,那么就是二月的最后一天,此时的day就是当前年二月的天数
public class ClassDemo {
public static void main(String[] args) {
System.out.println("2013年2月的天数:"+getErYueDay(2013));
}
public static int getErYueDay(int year){
Calendar c=Calendar.getInstance();
c.set(year, 02,01);//设置年月日
c.add(Calendar.DAY_OF_MONTH,-1);//Day -1
int day=c.get(Calendar.DAY_OF_MONTH);
return day;
}
}
结果:
2013年2月的天数:28
我们还有一个方法就是判断闰年和平年,闰年二月29天,平年28天
4. Math
public class ClassDemo {
public static void main(String[] args) {
double a=23.4;
System.out.println("大于"+a+"的最小整数是:"+Math.ceil(a));
System.out.println("小于"+a+"的最大整数是:"+Math.floor(a));
System.out.println("最接近"+a+"的double数"+Math.round(a));
System.out.println("返回0~1之间的随机数不包含1的数:"+Math.random());
Random r=new Random();
System.out.println("返回0~1之间的随机数不包含1的数:"+r.nextDouble());
System.out.println("返回一个整数随机数:"+r.nextInt());
System.out.println("返回一个小于50的整数随机数:"+r.nextInt(50));
}
}
结果:
大于23.4的最小整数是:24.0
小于23.4的最大整数是:23.0
最接近23.4的double数23
返回0~1之间的随机数不包含1的数:0.31784177008869685
返回0~1之间的随机数不包含1的数:0.8839091521841245
返回一个整数随机数:-282379189
返回一个小于50的整数随机数:26
5. 练习
把一个小数保留两位小数
public class ClassDemo {
public static void main(String[] args) {
double d = 23.2872;
/* 第一种 */
int d1 = (int) (d * 100);
System.out.println(d1 *0.01);
/*第二种:转换成字符串*/
String s=new Double(d).toString();
if(s.contains(".")){
System.out.println(s.substring(0, s.indexOf(".") + 3));
}
/*第三种*/
DecimalFormat df=new DecimalFormat("#.00");
System.out.println(df.format(d));
}
}
结果:
23.28
23.28
23.29
分享到:
相关推荐
解决windows7使用远程桌面“系统找不到指定的文件 C:\WINDOWS\system32\<LANG_NAME>\mstsc.exe.MUI”的问题 win7 64位 远程桌面 mstsc相关文件打包 包括systwm32 下搜索 mstsc 全部文件,压缩包解压之后,放在C:\...
Practical Mod Perl<br><br> Copyright <br> Preface <br> What You Need to Know <br> Who This Book Is For <br> How This Book Is Organized <br> Reference Sections <br> Filesystem Conventions <br> Apache ...
<systemPath>自己创建的存放db2jcc4.jar路径</systemPath> </dependncy> <dependency> <groupId>com.ibm.db2</groupId> <artifactId>db2jcc_license_cu</artifactId> <version>1.0.0</version> <scope...
在web.config中设置httpHandleer标签<br> <configuration><br> <system.web><br> <httpHandlers><br> <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory,Ajax" /><br> </httpHandlers><br> </...
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </...
The KDevelop Programming Handbook<br><br>1. Introduction<br>1.1. What you should know already<br>1.2. About this Handbook<br>1.3. Additional Information<br>2. The KDE and Qt Libraries<br>2.1. The Qt ...
<br> }<br> private<br>void BackUpDB()<br> {<br><br> string selfName =<br>this.txtSavePath.Text.Trim() +<br>@"<br>"<br>+<br>this.cboDatabase.Text.Trim() +<br>"_"+ System.DateTime.Now.ToString(...
COleDataSourceEx.exe<br>Simplifying Clipboard Rendering with the COleDataSourceEx Class.<br>剪贴板数据处理类(88KB)<END><br>61,PasteFNames.zip<br>Copying File Names from Explorer<br>从Explorer中拷贝...
APP管理,可以审核软件和下载软件,使用SSM+mven写入<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>...
<END><br>37 , LogOff.zip<br>This conveniently placed program allows you to log off from your system tray.<END><br>38 , ChPass.zip<br>Changes NT Domain Passord. This program has two compenants... a ...
23.zip<br>Dialog with Splash Screen Example Code...<br>Splash对话框的例子(18KB)<END><br>24,24.zip<br>Class to select directory<br>选择目录的类(13KB)<END><br>25,25.zip<br>Class to select directory ...
<br> foreach (Control dr in this.Controls)<br> {<br> if (dr.GetType().ToString() == "System.Windows.Forms.TextBox ")<br> {<br> this.Text = "";<br> }<br> }<br> }<br> else<br> {<br> MessageBox.Show(...
1,01.zip<br>Output<br>显示所有的调试信息(5KB)<END><br>2,02.zip<br>Some general debugging tips<br>一般的调试技巧(11KB)<END><br>3,03.zip<br>Debugging ISAPI extension<br>调试ISAPI扩展(4KB)<END><br>4,04....
that allows you to select a folder.<END><br>87,browsfil.zip<br>A text box type control, that allows you to select a file..<END><br>88,colbrwse.zip<br>A colour select control.<END><br>89,colorsel.zip...
<br> } <br> private void notifyIcon_DBClick(object sender, System.EventArgs e)<br> {<br> //<br> }<br><br> protected override void Dispose( bool disposing )<br> {<br> if( disposing )<br> {...
System.Runtime.Serialization.dll System.Runtime.Serialization.dll System.Runtime.Serialization.dll
syscolor.zip<br>This is an example of how to retrieve the system colors and display them on your form.<END><br>13 , findfiles.zip<br>This is an example of how to return all of the files on a users ...
11.zip<br>CAutoRichEditCtrl - automate rich edit formatting and RTF handling.<br>自动格式化RTF的继承类CAutoRichEditCtrl(62KB)<END><br><br>1,02.zip<br>Adding a Control to the Property Sheet <br>在属性...
- <member name="M:Pic_Chart_Load.Control_add.Dgv_Add_Date(System.Windows.Forms.DataGridView,System.Windows.Forms.DataGridView,System.Windows.Forms.CheckBox,System.Windows.Forms.CheckBox,System.Windows...
6 , chunks.zip<br>This will open a file and read it in "Chunks" of a selected file.<END><br>7 , logging.zip<br>This is a bas that will log installation procedures so the file can be removed later.<END...