真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java中簡(jiǎn)單的函數(shù)代碼 java語言常用函數(shù)大全

給一個(gè) JAVA代碼 要求里面有 一些 基本的常用函數(shù)和注釋

public?class?Display?{

目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、宜陽網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

//定義ing型變量a

public?int?a?;

//獲取a的值

public?int?getA()?{

return?a;

}

//設(shè)置a的值

public?void?setA(int?a)?{

this.a?=?a;

}

public?static?void?main(String[]?args){

Display?dis?=?new?Display();//定義一個(gè)對(duì)象dis

dis.setA(10);//為dis對(duì)象的成員變量a賦值為10

System.out.println("輸出int型變量a的值:"?+?dis.getA());

}

}

Java語言里面如何調(diào)用函數(shù),請(qǐng)給出一段調(diào)用函數(shù)的代碼?

public class HelloWord{

//自定義的輸出函數(shù),打印字符串helloword!

public void printHelloWord() {

System.out.println("helloword!");

}

public static void main(String args[]){

//創(chuàng)建對(duì)象實(shí)例

HelloWord hw = new HelloWord();

//通過對(duì)象實(shí)例調(diào)用自己的成員函數(shù)

hw.printHelloWord() ;

}

}

你看看滿意不?

求一段簡(jiǎn)單的java代碼 寫一個(gè)簡(jiǎn)單的函數(shù)比如y=sin(x) 然后調(diào)用該函數(shù)得到結(jié)果y(pi/2)=1

幫你寫好了.

public class AAA {

public static void main (String[] args){

double PI, y;

PI = 4 * Math.atan(1.0);

y = F(0.5*PI);

System.out.println(y);

}

public static double F (double x){

return Math.sin(x);

}

}

java中讀字符的函數(shù)代碼!

/**

* @version 1.0

* @author 韓衛(wèi)召

*

* 多種方式讀文件的內(nèi)容

* 按字節(jié)讀取文件內(nèi)容,按字符讀取文件的內(nèi)容,按行讀取文件的內(nèi)容,隨即讀取文件的內(nèi)容

*/

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.RandomAccessFile;

import java.io.Reader;

import java.util.ResourceBundle;

public class ReadFromFile {

/**

* 以字節(jié)為單位讀取文件的內(nèi)容,常用于二進(jìn)制文件,如聲音,圖象,影象等文件

*

* @param filename

* 文件名

*/

public static void readFileByBytes(java.lang.String filename) {

File file = new File(filename);

InputStream in = null;

System.out.println("以字節(jié)為單位讀取文件的內(nèi)容,一次讀一個(gè)字節(jié): ");

// 一次讀一個(gè)字節(jié)

try {

in = new FileInputStream(file);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

int tempbyte;

try {

// 不斷的讀取,直到文件結(jié)束

while ((tempbyte = in.read()) != -1) {

System.out.write(tempbyte);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

in.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return;

}

System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀多個(gè)字節(jié): ");

// 一次讀取多個(gè)字節(jié)

byte[] tempbytes = new byte[100];

int byteread = 0;

try {

in = new FileInputStream(filename);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ReadFromFile.showAvailabelBytes(in);

try {

while ((byteread = in.read(tempbytes)) != -1) {

// 讀取多個(gè)字節(jié)到數(shù)組中,byteead為一次讀取的字節(jié)數(shù)

System.out.write(tempbytes, 0, byteread);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void readFileByteChars(java.lang.String filename) {

/**

* 以字符為單位讀取文件,常用與讀文本,數(shù)字等類型的文件

*/

File file = new File(filename);

Reader reader = null;

System.out.println("以字符為單位讀取文件內(nèi)容,一次讀一個(gè)字節(jié): ");

// 一次讀一個(gè)字符

try {

reader = new InputStreamReader(new FileInputStream(file));

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

int tempchar;

try {

while ((tempchar = reader.read()) != -1) {

// 在Window下,\r\n這兩個(gè)字符在一起時(shí),表示一個(gè)換行

// 但如果這兩個(gè)字符分開顯示時(shí),會(huì)換行兩次行

// 因此,屏蔽掉\r,或者\(yùn)n;否則,將會(huì)多出來很多空行

if (((char) tempchar) != '\r') {

System.out.println((char) tempchar);

}

}

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("以字符為單位讀取文件內(nèi)容,一次讀多個(gè)字符: ");

char[] tempchars = new char[30];

int charread = 0;

try {

reader = new InputStreamReader(new FileInputStream(filename));

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

// 讀入多個(gè)字符到字符數(shù)組中,charread為一次讀取字符數(shù)

while ((charread = reader.read(tempchars)) != -1) {

if ((charread == tempchars.length)

(tempchars[tempchars.length - 1] != '\r')) {

System.out.println(tempchars);

} else {

for (int i = 0; i charread; i++) {

if (tempchars[i] == '\r') {

continue;

} else {

System.out.print(tempchars[i]);

}

}

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

}

}

}

}

public static void readFileByLines(java.lang.String filename) {

File file = new File(filename);

BufferedReader reader = null;

// System.out.println("以行為單位讀取文件的內(nèi)容,一次讀一整行: ");

try {

reader = new BufferedReader(new FileReader(file));

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

java.lang.String tempString = null;

int line = 1;

try {

while ((tempString = reader.readLine()) != null) {

if(tempString.indexOf("福州")!=-1){

System.out.println("靠!出錯(cuò)了!趕緊報(bào)警??! " + line + ": " + tempString);

}

// System.out.println("line " + line + ": " + tempString);

line++;

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public static void readFileByRandomAccess(java.lang.String filename) {

RandomAccessFile randomFile = null;

System.out.println("隨即讀取一段文件內(nèi)容: ");

try {

randomFile = new RandomAccessFile(filename, "r");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

long fileLength = 0;

try {

fileLength = randomFile.length();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

int beginIndex = (fileLength 4) ? 4 : 0;

try {

randomFile.seek(beginIndex);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

byte[] bytes = new byte[10];

int byteread = 0;

try {

while ((byteread = randomFile.read(bytes)) != -1) {

System.out.write(bytes, 0, byteread);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (randomFile != null) {

try {

randomFile.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

private static void showAvailabelBytes(InputStream in) {

try {

System.out.println("當(dāng)前字節(jié)輸入流中的字節(jié)數(shù)為: " + in.available());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(java.lang.String args[]) {

ResourceBundle rb = ResourceBundle.getBundle("PreventTampering");

String filePath = rb.getString("filePath");

java.lang.String filename = filePath+"test.txt";

System.out.println(filename);

// ReadFromFile.readFileByBytes(filename);

// ReadFromFile.readFileByteChars(filename);

ReadFromFile.readFileByLines(filename);

// ReadFromFile.readFileByRandomAccess(filename);

}

java常用函數(shù)方法

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)鍵值,返回索引.沒有的話返回-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 從字符串到日期類型的轉(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)成日期類型

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("日期類型:"+dNow);

}

}

4 獲取系統(tǒng)時(shí)期和時(shí)間,轉(zhuǎn)換成SQL格式后更新到數(shù)據(jù)庫

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ù)庫,仍然要用rs.updateString,而不是rs.updateDade。

rs.updateString("regtime",dateStr); //regtime字段為datetime類型的

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ù)庫里的日期只以年-月-日的方式輸出

定義日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);

sql語句為:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

輸出:

System.out.println(df.format(rs.getDate("bookDate")));


網(wǎng)站名稱:java中簡(jiǎn)單的函數(shù)代碼 java語言常用函數(shù)大全
瀏覽路徑:http://weahome.cn/article/hhphie.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部