- 日期
- 匿名类
- lambda
- stringBuffer stringBuilder
- 定时
日期
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main (String[] args) {
Date date = new Date(); // 当前日期
System.out.println(date.toString());
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime().toString());
int year = calendar.get(Calendar.YEAR); // 年
int month = calendar.get(Calendar.MONTH) + 1; // 月
int cdate = calendar.get(Calendar.DATE); // 日
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 时
int minute = calendar.get(Calendar.MINUTE); // 分
int second = calendar.get(Calendar.SECOND); // 秒
int day = calendar.get(Calendar.DAY_OF_WEEK); // 星期,1为星期日、2为星期1,以此类推
Calendar date1 = Calendar.getInstance(); // 日期比较
Calendar date2 = Calendar.getInstance();
date1.set(2017, 1, 1);
date2.set(2018, 1, 1);
if (date1.before(date2)) { System.out.println('date1在date2之前'); }
}
}
匿名类
interface IDatabase { void connect (String connString); }
public class Main {
public static void main (String[] args) {
// 线上类
class Oracle implements IDatabase {
@Override
public void connect (String connString) { System.out.println('连接Oracle'); }
}
IDatabase oracle = new Oracle();
oracle.connect('ip/uid/pwd');
// 匿名类
IDatabase mysql = new IDatabase() {
@Override
public void connect (String connString) { System.out.println('连接MySQL'); }
};
mysql.connect('ip/uid/pwd');
}
}
lambda
import java.util.function.BiFunction; // 方法型接口(java.util.function)
import java.util.function.Function;
import java.util.HashMap; // HashMap
import java.util.Map;
interface IDatabase { void update (); } // 单方法无参数接口
public class Main {
public static void main (String[] args) {
// Lambda 与单方法无参数接口
IDatabase blogDB = () -> { System.out.println('更新博客数据库'); };
blogDB.update();
// Lambda 与方法型接口 Function<T:参数类型,R:返回值类型>
Function<Integer, Integer> multi2 = (i) -> { return i * 2; };
System.out.println(multi2.apply(10));
// Lambda 与方法型接口 BiFunction<T:第1参数类型,U:第2参数类型,R:返回值类型>
BiFunction<Integer, Integer, Integer> addfunc = (a, b) -> { return a + b; };
System.out.println(addfunc.apply(10, 20));
// Lambda 与 HashMap 使用
Map<String, String> stringMap = new HashMap<>();
stringMap.put('usa', '美国');
stringMap.put('chn', '中国');
for (Map.Entry<String, String> item : stringMap.entrySet()) { System.out.println('.' + item.getKey() + ':' + item.getValue()); }
stringMap.forEach((key, value) -> { System.out.println('.' + key + ':' + value); });
}
}
stringBuffer stringBuilder
public class Main {
public static void main (String[] args) {
StringBuffer stringBuffer = new StringBuffer(); // StringBuffer 线程安全
stringBuffer.append('我学习Java');
stringBuffer.append('我学习Android');
System.out.println(stringBuffer.toString());
StringBuilder stringBuilder = new StringBuilder(); // StringBuilder 速度优势
stringBuilder.append('我学习Java');
stringBuilder.append('我学习Android');
System.out.println(stringBuilder.toString());
}
}
定时
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Main {
public static void main (String[] args) {
SimpleDateFormat format = new SimpleDateFormat('[HH:mm:ss]'); // 日期格式化输出
class MyTimerTask extends TimerTask {
@Override
public void run () { System.out.println(format.format(new Date()) + 'MyTimerTask.run()...'); }
}
Timer timer = new Timer();
timer.schedule(new MyTimerTask(), 1000, 3000);
}
}
网友评论