package day14_Test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
public class Demo {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
System.out.println(stringToDate("2015_09_22 20:17:12","yyyy_MM_dd HH:mm:ss"));
}
// 二分法查找
static int twoMethod(int arr[], int num) {
int start = 0;
int end = arr.length - 1;
int mid = (start + end) / 2;
while (true) {
if (arr[mid] == num) {
return mid;
} else if (arr[mid] > num) {
end = mid - 1;
} else {
start = mid + 1;
}
mid = (start + end) / 2;
if (start > end)
return -1;
}
}
// 冒泡排序
static void MpSort(int arr[]) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1])
arr[j] = arr[j + 1] + arr[j] - (arr[j + 1] = arr[j]);
}
}
}
// 选择排序
static void XzSort(int arr[]) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] > arr[i])
arr[i] = arr[i] + arr[j] - (arr[j] = arr[i]);
}
}
}
/*
* 把字符串"-34 29 76 11 27"中的数据排序并输出。 String:切割 public String[] split(String
* regex) 将字符串按照指定内容切割成若干小字符串 Integer:由String转为int public static int
* parseInt(String s) throws NumberFormatException 根据字符串返回int值
*/
static void splitString() {
String str = "-34 29 76 11 27";
String arr[] = str.split(" ");
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
if ((arr[i].compareTo(arr[j])) > 0) {
String temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
static void integerString() {
String str = "-34 29 76 11 27";
String arr1[] = str.split(" ");
int arr[] = new int[arr1.length];
for (int i = 0; i < arr1.length; i++)
arr[i] = Integer.decode(arr1[i]);
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
// math类
static void mathClass() {
System.out.println(Math.abs(-100));
System.out.println(Math.ceil(-1.98));
}
/*
* 求0-100的随机数 求1-100的随机数 求50-100的随机数 求80-120的随机数 求m-n的随机数,定义方法
*
* public int nextInt(int n) 生成指定区间的随机数 [0,n)
*/
static int randomClass(int m, int n) {
if (m > n)
m = m + n - (n = m);
Random random = new Random();
int x;
while (true) {
x = random.nextInt(n + 1);
if (x >= m)
return x;
}
}
// 获取指定年份的二月的天数
static int calendarClass(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, 2, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.get(calendar.DAY_OF_MONTH);
}
// 400计算方法获得二月天数 被4整除且不能被100整除 或者能被400整除
//DateFormat
static String dateToString(Date date,String format){
return new SimpleDateFormat(format).format(date);
}
static Date stringToDate(String date,String format) throws ParseException{
return new SimpleDateFormat(format).parse(date);
}
//获取时间指定时间到当前时间的天数
static long getTime(Date date){
return (System.currentTimeMillis()-date.getTime())/1000/60/60/24;
}
} |
|