2 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
Java的一些常用函數(shù),方法總結(jié):
1:數(shù)組方面:
數(shù)組是一個(gè)對(duì)象,數(shù)組名可以理解和C++里面一樣,
1):System.arraycopy(src,srcindex,dst,dstindex,src.length);
2):java.util.Arrays.sort(數(shù)組名);//塊數(shù)排序法進(jìn)行排序
3):java.util.Arrays.binarySearch(arr,key);//從哪里收,關(guān)鍵值,返回索引.沒(méi)有的話返回-1
4):java.util.Array.fill(arr,value),設(shè)置數(shù)組初始值
5):Array.equals(arr1,arr2)//比較兩個(gè)數(shù)組中元素是不是全部相等,是返回true,不是返回false
2:時(shí)間方面:
public static String dateToStr(java.util.Date date)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date);
return str;
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateUtil
{
public static java.util.Date parseDate(String dateStr, String format)
{
java.util.Date date = null;
try
{
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
String dt = dateStr.replaceAll("-", "/");
if ((!dt.equals("")) && (dt.length() < format.length()))
{
dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0");
}
date = (java.util.Date) df.parse(dt);
}
catch (Exception e)
{
}
return date;
}
public static java.util.Date parseDate(String dateStr)
{
return parseDate(dateStr, "yyyy/MM/dd");
}
public static String format(java.util.Date date, String format)
{
String result = "";
try
{
if (date != null)
{
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
result = df.format(date);
}
}
catch (Exception e)
{
}
return result;
}
public static String format(java.util.Date date)
{
return format(date, "yyyy/MM/dd");
}
public static int getYear(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.YEAR);
}
public static int getMonth(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MONTH) + 1;
}
public static int getDay(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.DAY_OF_MONTH);
}
public static int getHour(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.HOUR_OF_DAY);
}
public static int getMinute(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MINUTE);
}
public static int getSecond(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.SECOND);
}
public static long getMillis(java.util.Date date)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
public static String getDate(java.util.Date date)
{
return format(date, "yyyy/MM/dd");
}
public static String getTime(java.util.Date date)
{
return format(date, "HH:mm:ss");
}
public static String getDateTime(java.util.Date date)
{
return format(date, "yyyy/MM/dd HH:mm:ss");
}
public static java.util.Date addDate(java.util.Date date, int day)
{
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
public static int diffDate(java.util.Date date, java.util.Date date1)
{
return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
}
public static String getMonthBegin(String strdate)
{
java.util.Date date = parseDate(strdate);
return format(date, "yyyy-MM") + "-01";
}
public static String getMonthEnd(String strdate)
{
java.util.Date date = parseDate(getMonthBegin(strdate));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_YEAR, -1);
return formatDate(calendar.getTime());
}
public static String formatDate(java.util.Date date)
{
return formatDateByFormat(date, "yyyy-MM-dd");
}
public static String formatDateByFormat(java.util.Date date, String format)
{
String result = "";
if (date != null)
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
result = sdf.format(date);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return result;
}
}
Java日期格式化及其使用例子
1 SimpleDateFormat擔(dān)當(dāng)重任,怎樣格式化都行
import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo
{
public static void main(String[] args)
{
Date now=new Date();
SimpleDateFormat f=newSimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk點(diǎn)mm分");
System.out.println(f.format(now));
f=new SimpleDateFormat("a hh點(diǎn)mm分ss秒");
System.out.println(f.format(now));
}
}
2 從字符串到日期類(lèi)型的轉(zhuǎn)換:
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.text.*;
publicclass Demo
{
public static void main(String[] args)
{
String strDate="2005年04月22日";
//注意:SimpleDateFormat構(gòu)造函數(shù)的樣式與strDate的樣式必須相符
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");
//必須捕獲異常
try
{
Date date=simpleDateFormat.parse(strDate);
System.out.println(date);
}
catch(ParseException px)
{
px.printStackTrace();
}
}
}
3 將毫秒數(shù)換轉(zhuǎn)成日期類(lèi)型
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.text.*;
public class Demo
{
public static void main(String[] args)
{
long now=System.currentTimeMillis();
System.out.println("毫秒數(shù):"+now);
Date dNow=new Date(now);
System.out.println("日期類(lèi)型:"+dNow);
}
}
4 獲取系統(tǒng)時(shí)期和時(shí)間,轉(zhuǎn)換成SQL格式后更新到數(shù)據(jù)庫(kù)
java.util.Date d=new java.util.Date(); //獲取當(dāng)前系統(tǒng)的時(shí)間
//格式化日期
new java.text.SimpleDateFormat s= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = s.format(d); //轉(zhuǎn)為字符串
使用RS更新數(shù)據(jù)庫(kù),仍然要用rs.updateString,而不是rs.updateDade。
rs.updateString("regtime",dateStr); //regtime字段為datetime類(lèi)型的
5 按本地時(shí)區(qū)輸出當(dāng)前日期
Date myDate = new Date();
System.out.println(myDate.toLocaleString());
輸出結(jié)果為:
2003-5-30
6 如何格式化小數(shù)
DecimalFormat df = new DecimalFormat(",###.00");
double aNumber = 33665448856.6568975;
String result = df.format(aNumber);
Sytem. out.println(result);
輸出結(jié)果為:
33,665,448,856.66
其他:獲取毫秒時(shí)間 System.currentTimeMillis();
7 在數(shù)據(jù)庫(kù)里的日期只以年-月-日的方式輸出
定義日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);
sql語(yǔ)句為:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";
輸出:
System.out.println(df.format(rs.getDate("bookDate")));

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
1 int d=Integer.preInt(a);
2 int index=b.firstindexOf("你");
String e=b.subString(index-1,index);
3equals() 即可
4String c2=c.toString();
int length=c2.lenght();
5
String [] c=f.trim().split(" ");
String e="";
for(int i=0;i<c.length;i++){
if(!c[i].equals("")){
d+=c[i];
}
}
添加回答
舉報(bào)