java.text
类 Format
Format
是一个用于格式化语言环境敏感的信息(如日期、消息和数字)的抽象基类。
Format
定义了编程接口,用于将语言环境敏感的对象格式化为
String
(使用
format
方法)和将
String
重新解析为对象(使用
parseObject
方法)。
通常,一个 format 的
parseObject
方法必须能解析任何由其
format
方法格式化的字符串。不过,也可能存在不能解析的异常情况。例如,
format
方法可能创建中间无分隔符的两个相邻整数,在这种情况下,
parseObject
无法判断哪个数字属于哪个数。
SimpleDateFormat
是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
/**
* 把符合日期格式的字符串转换为日期类型
*/
public static java.util.Date stringtoDate(String dateStr, String format) {
Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
formater.setLenient( false);
d = formater.parse(dateStr);
} catch (Exception e) {
// log.error(e);
d = null;
}
return d;
}
/**
* 把日期转换为字符串
*/
public static String dateToString(java.util.Date date, String format) {
String result = "";
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
result = formater.format(date);
} catch (Exception e) {
// log.error(e);
}
return result;
}
/**
* 获得某月的天数
*/
public static int getDaysOfMonth(String year, String month) {
int days = 0;
if (month.equals( "1") || month.equals( "3") || month.equals( "5")
|| month.equals( "7") || month.equals( "8") || month.equals( "10")
|| month.equals( "12")) {
days = 31;
} else if (month.equals( "4") || month.equals( "6") || month.equals( "9")
|| month.equals( "11")) {
days = 30;
} else {
if ((Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0)
|| Integer.parseInt(year) % 400 == 0) {
days = 29;
} else {
days = 28;
}
}
return days;
}
* 把符合日期格式的字符串转换为日期类型
*/
public static java.util.Date stringtoDate(String dateStr, String format) {
Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
formater.setLenient( false);
d = formater.parse(dateStr);
} catch (Exception e) {
// log.error(e);
d = null;
}
return d;
}
/**
* 把日期转换为字符串
*/
public static String dateToString(java.util.Date date, String format) {
String result = "";
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
result = formater.format(date);
} catch (Exception e) {
// log.error(e);
}
return result;
}
/**
* 获得某月的天数
*/
public static int getDaysOfMonth(String year, String month) {
int days = 0;
if (month.equals( "1") || month.equals( "3") || month.equals( "5")
|| month.equals( "7") || month.equals( "8") || month.equals( "10")
|| month.equals( "12")) {
days = 31;
} else if (month.equals( "4") || month.equals( "6") || month.equals( "9")
|| month.equals( "11")) {
days = 30;
} else {
if ((Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0)
|| Integer.parseInt(year) % 400 == 0) {
days = 29;
} else {
days = 28;
}
}
return days;
}
2)
java dos输入输出
import java.util.Scanner;
public class DaoXu {
public static void main(String[] args) {
// TODO 自动生成方法存根
System.out.println( "请输入单个字符并回车: ");
Scanner c = new Scanner(System.in);
String[] ch = new String[5];
for ( int i = 0; i < 5; i++) {
ch[i] = c.next();
}
// Arrays.sort(ch);
System.out.print( "倒序输出: ");
for ( int j = ch.length - 1; j >= 0; j--) {
System.out.print(ch[j] + " ");
}
}
}
public class DaoXu {
public static void main(String[] args) {
// TODO 自动生成方法存根
System.out.println( "请输入单个字符并回车: ");
Scanner c = new Scanner(System.in);
String[] ch = new String[5];
for ( int i = 0; i < 5; i++) {
ch[i] = c.next();
}
// Arrays.sort(ch);
System.out.print( "倒序输出: ");
for ( int j = ch.length - 1; j >= 0; j--) {
System.out.print(ch[j] + " ");
}
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class IP extends JFrame implements ActionListener {
private static final long serialVersionUID = 3339481369781127417L;
JButton jb1;
JButton jb2;
JButton jb3;
JPanel jp;
JLabel jl;
JLabel jl1;
JTextField jt;
public IP() {
this.jp = new JPanel();
this.jl = new JLabel();
this.jl1 = new JLabel( "您的域名:");
this.jb1 = new JButton( "提交");
this.jb2 = new JButton( "重置");
this.jb3 = new JButton( "退出");
this.jt = new JTextField(20);
this.jb1.addActionListener( this);
this.jb2.addActionListener( this);
this.jb3.addActionListener( this);
this.jp.setLayout( new GridLayout(3, 2));
this.jp.add( this.jl1);
this.jp.add( this.jt);
this.jp.add( this.jb1);
this.jp.add( this.jl);
this.jp.add( this.jb2);
this.jp.add( this.jb3);
setBounds(200, 200, 500, 240);
add( this.jp);
setVisible( true);
setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new IP();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.jb1) {
String url = this.jt.getText();
InetAddress ip = null;
try {
ip = InetAddress.getByName(url);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
this.jl.setText(ip.toString());
} else if (e.getSource() == this.jb2) {
this.jl.setText("");
this.jt.setText("");
} else {
System.exit(0);
}
}
}
转载于:https://blog.51cto.com/2301703/1035797