码迷,mamicode.com
首页 > 编程语言 > 详细

java基础(一)

时间:2016-07-19 18:34:44      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }
  }
}

java基础(一)

标签:

原文地址:http://www.cnblogs.com/wanshoushou/p/5685642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!