定义参数可变的 方法;
重载
构造器中调用其他构造器
super限定调用父类方法
初始化模块
java tostring()方法
java变量 分为 运行时和编译时 变量。 比如特殊的string a = "ssss";在编译时 就确定了。 而 new string() 就只能在运行时才知道
单例类
处理行为 到运行时才知道。 用接口实现
枚举 enum
枚举使用接口
实例化接口使用
生成接口对象 Gender g = Gender.valueof("MALE");
或者直接使用 Gender.MALE.getName();
格式字符串:
yyyy-MM-dd HH:mm:ss 其中大写的必须是大写
java中生成时间一般用 calendar 类 因为 date类有缺陷 使用方法如下
java 国际化
Locale[] locales = Locale.getAvailableLocales();
for (Locale locale : locales) {
System.out.println(locale.getDisplayCountry()+"="+locale.getCountry()+" "+
locale.getDisplayLanguage()+"="+locale.getLanguage());
}
Locale myLocale = Locale.US;
System.out.println(myLocale.getDisplayCountry());
ResourceBundle resourceBundle = ResourceBundle.getBundle("I18n/mess", myLocale);
System.out.println(resourceBundle.getString("hello"));
国际化文件,目录及其名字
//数字格式化,用处货币的格式化
double db = 5432123.45;
//创建4个locale 分别代表中国,日本,德国,美国
Locale[] locales2 ={Locale.CHINA,Locale.JAPAN,Locale.GERMAN,Locale.US};
//为上面四个locale 创建12个 numberformat 每个 对应数值格式器 百分数格式器,货币格式器
NumberFormat[] nf = new NumberFormat[12];
for(int i=0; i<locales2.length;i++){
nf[i*3] = NumberFormat.getNumberInstance(locales2[i]);//数值
nf[i*3+1] = NumberFormat.getPercentInstance(locales2[i]);//百分数
nf[i*3+2] = NumberFormat.getCurrencyInstance(locales2[i]);//货币
}
for(int i=0; i<locales2.length;i++){
String tip = i==0 ? "---中国货币=---": i==1 ? "---日本货币---":i==2 ?"---德国货币---":"---美国货币---";
System.out.println(tip);
System.out.println("通用数字格式"+nf[i*3].format(db));
System.out.println("百分比数字格式"+nf[i*3+1].format(db));
System.out.println("货币数字格式"+nf[i*3+2].format(db));
}
效果
类似的 还有
dateformat simpledateformat
BigDecimal 计算金额是使用这个类 不会发生精度损失
hashset
bigdecimal tostring
toPlainString 区别
对于 BigDecimal b ; (b=(0.4321)^ 20)
String s = b.toPlainString() ;
System.out.println(s) ;
输出为:
0.00000005148554641076956121994511276767154838481760200726351203835429763013462401
若String s = b.toString() ;
输出为:
5.148554641076956121994511276767154838481760200726351203835429763013462401E-8
String s = b.toPlainString() ;
System.out.println(s) ;
输出为:
0.00000005148554641076956121994511276767154838481760200726351203835429763013462401
若String s = b.toString() ;
输出为:
5.148554641076956121994511276767154838481760200726351203835429763013462401E-8
得到输入月份的 最后一天
String date = "2017-05";
int len = date.length();
if (len == 7) {
date += "-01";
}
if (!ValidateUtil.isDate(date)) {
throw new AppException("传入日期格式不正确!");
}
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
date = format.format(format.parse(date));
System.out.println(date);
date = date.substring(0, 7);
format = new SimpleDateFormat("yyyy-MM");
Date d = format.parse(date);
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println(c.get(Calendar.YEAR));//得到 时间c中的年份 如果是月份的话 范围 0到11
int m = c.get(Calendar.MONTH);
switch (m) {
case 3:
case 5:
case 8:
case 10:
date += "-30";
break;
case 1:
int year = c.get(Calendar.YEAR);
//这里差一个 判断是不是瑞年
date += "-28";
break;
default:
date += "-31";
}
System.out.println(date);
计算两个二月份间有多少天
while (s_c.compareTo(e_c) < 0) {//比较两个时间 s开始时间 e结束时间
if (type == 2) {
if (isWorkDay(format.format(s_c.getTime())))
days++;
} else {
days++;
}
s_c.add(Calendar.DAY_OF_MONTH, 1);//
}