标签:
1.在java中,关于时间的转化问题
JDK 1.1以后开始使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和解析日期字符串。
Calendar cal = Calendar.getInstance();
###Gets a calendar using the default time zone and locale.
cal.add(Calendar.DAY_OF_MONTH, -1);
###Adds the specified (signed) amount of time to the given calendar field,based on the calendar‘s rules.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
###Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default locale.
String str = format.format(cal.getTime())
###Formats a Date into a date/time string
Date date = df.parse(str);
###Parses text from the beginning of the given string to produce a date.
Date date = new Date();
###Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
date.after(Date when);
###Tests if this date is after the specified date.
date.before(Date when);
###Tests if this date is before the specified date.
2.在java中,实现集合中元素的比较排序,可以使用Comparator接口或Comparable接口
两个接口的区别:
Comparable位于包java.lang下,而Comparator位于包java.util下。Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序。
Comparator采用策略模式(strategy design pattern),不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。
2.1 Comparable简单使用
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
return age > o.getAge() ? 1 : -1;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("a",22));
list.add(new Student("b",26));
list.add(new Student("c",24));
list.add(new Student("d",24));
Collections.sort(list);
for(Student s : list){
System.out.println(s);
}
}
}
2.2 Comparator简单使用
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class MyComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() > o2.getAge() ? -1 : 1;
}
}
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("a",22));
list.add(new Student("b",26));
list.add(new Student("c",24));
list.add(new Student("d",24));
MyComparator comparator=new MyComparator();
Collections.sort(list, comparator);
for(Student s : list){
System.out.println(s);
}
}
}
标签:
原文地址:http://www.cnblogs.com/wanshoushou/p/5685642.html