Java Basics (i)

Source: Internet
Author: User

1. In Java, the issue of time conversion
JDK 1.1 began using the Calendar class to convert between date and Time fields, using the DateFormat class to format and parse date strings.
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 represents the time at which it is allocated, measured to the NE Arest millisecond.
Date.after (Date when);
# # #Tests If this date was after the specified date.
Date.before (Date when);
# # #Tests If this date is before the specified date.

2. In Java, you can use the comparator interface or the comparable interface to implement a comparison ordering of the elements in the collection
The difference between the two interfaces:
Comparable is located under Package Java.lang, while comparator is under package java.util. Comparable is the sort of method implementation defined within the collection, and Comparator is the sort implemented outside the collection.
Comparator uses the policy mode (strategy design pattern), does not change the object itself, and uses a policy object (strategy object) to change its behavior.

2.1 Comparable simple use
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.) {
This.age = age;
  
@Override
Public int compareTo (Student o) {
return > 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 simple to use
public class Student {
private String name;
private int age;
Public Student (String name, int.) {
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.) {
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 Basics (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.