···
package com.znsd.jsp.demo;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Demo {
public static void main(String[] args) throws ParseException {
/*Locale locale = Locale.US;
System.out.println(locale);
ResourceBundle rb = ResourceBundle.getBundle("message", locale);
System.out.println(rb.getString("name"));*/
// test();
// number();
// messageFormat();
/Demo d = new Demo();
Demo dd = new Demo();
System.out.println(d == dd);/
// format(“¥1.23,¥2.22,¥6.1”,“¥1”);
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd”);
String s = “Thu Oct 01 11:37:16 GMT+08:00 2009”;
Date d = df.parse(s);
System.out.println(d);
String ss = df.format(d);
System.out.println(ss);
/*Date d = new Date();
Date s = df.parse("2019-03-25 16:34:18");
System.out.println(d);
System.out.println(s.toString());*/
}
public static void test()
{
Date date = new Date(); // 当前这一刻的时间(日期、时间)
// 输出日期部分
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL,Locale.GERMAN);
String result = df.format(date);
System.out.println(result);
// 输出时间部分
df = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINA);
result = df.format(date);
System.out.println(result);
// 输出日期和时间
df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG,Locale.CHINA);
result = df.format(date);
System.out.println(result);
// 把字符串反向解析成一个date对象
String s = "10-9-26 下午02时49分53秒";
df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG,Locale.CHINA);
try {
Date d = df.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
}
public static String format(String total2,String discount2)
{
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
String[] ss = total2.split(",");
String[] ss2 = discount2.split(",");
System.out.println(total2);
Double cc = 0.0;
Double dd = 0.0;
for (String sss : ss) {
Number n = 0;
try {
n = nf.parse(sss);
} catch (ParseException e) {
e.printStackTrace();
}
cc += n.doubleValue();
}
for (String sss : ss2) {
Number n = 0;
try {
n = nf.parse(sss);
} catch (ParseException e) {
e.printStackTrace();
}
dd += n.doubleValue();
}
System.out.println(cc);
String total = "¥"+cc;
String discount = "¥"+dd;
System.out.println(total);
System.out.println(discount);
nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
Number totalpay = 0;
Number realpay = 0;
try {
totalpay = nf.parse(total);
realpay = nf.parse(discount);
} catch (ParseException e) {
e.printStackTrace();
}
Double totalPrice = totalpay.doubleValue();
Double realpay1 = realpay.doubleValue();
double n = totalPrice - realpay1;
String back = "";
String nn = nf.format(n); //共节省
String nn2 = nf.format(realpay1); //商品总额
System.out.println(nn);
System.out.println(nn2);
return nn+","+nn2;
}
public static void number()
{
//数字显示为货币格式
double price = 89;
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
String result = nf.format(price);
System.out.println(result);
//货币字符串转换为数字
String s = "¥89.00";
nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
Number n;
try {
n = nf.parse(s);
System.out.println(n.doubleValue() + 1);
} catch (ParseException e) {
e.printStackTrace();
}
//小数转换为百分比
double num = 0.5;
nf = NumberFormat.getPercentInstance();
System.out.println(nf.format(num));
}
public static void messageFormat()
{
String str = "{0},{1},{2},{3},4,5";
String s = "{1}";
MessageFormat mf = new MessageFormat(str, Locale.getDefault());
MessageFormat mf1 = new MessageFormat(s, Locale.getDefault());
Object[] params = {0,1,2,3};
System.out.println(mf .format(params));
System.out.println(mf1.format(params));
}
}
···