顯示具有 Java 標籤的文章。 顯示所有文章
顯示具有 Java 標籤的文章。 顯示所有文章

2013年12月13日 星期五

【Android】Java日期


// 取得目前日期
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  
String currentDatetime = dateFormat.format(date);


// 日期-1
Calendar day = Calendar.getInstance(); 
day.add(Calendar.DATE,-1); 
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd "); 
System.out.println("yesterday =" + sdf.format(day.getTime()));


// String 轉日期
String currentDate = "2012-12-13";
Date date = sdf.parse(currentDate); // 解析
Calendar calendar = Calendar.getInstance(); // 呼叫Calendar
calendar.setTime(date); // 設定指定時間

int year=calendar.get(Calendar.YEAR); // 取得年月日
int month=calendar.get(Calendar.MONTH);
int day=calendar.get(Calendar.DAY_OF_MONTH);

String _year = Integer.toString(year); // 轉字串
String _month = Integer.toString(monthOfYear + 1);
String _day = Integer.toString(dayOfMonth);
String date = _year + "-" + _month + "-" + _day;

彈出日期選擇
void dateView(){
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
 try {
  //設定日期格式
  Date date = sdf.parse(currentDate);
  
  Calendar calendar = Calendar.getInstance();//取得目前時間
  calendar.setTime(date);//或是設定指定時間
  int year=calendar.get(Calendar.YEAR);
  int month=calendar.get(Calendar.MONTH);
  int day=calendar.get(Calendar.DAY_OF_MONTH);

  new DatePickerDialog(this,
    new DatePickerDialog.OnDateSetListener() {
     @Override
     public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
      // 設置日曆
      String _year = Integer.toString(year);
      String _month = Integer.toString(monthOfYear + 1);
      String _day = Integer.toString(dayOfMonth);
      String date = _year + "-" + _month + "-" + _day;
      currentDate = date;
      editDate.setText(date);
     }
    }, year, month, day).show();
  
 } catch (ParseException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } 
}

2013年10月7日 星期一

【職訓局-手機程式開發班】2013 10/07 java


1.繼承
2.抽象類別 abstract

3.介面 interface

4.內部類別

5.匿名的內部類別

2013年9月23日 星期一

【職訓局-手機程式開發班】2013 9/23 java

1.StringBuffer 與 StringBuilder

字串需一直變動使用String每變動一次都會產生新的物件,
會浪費記憶體,這時就要用StringBuffer與StringBuiler,
StringBuiler不能使用在多執行序。

StringBuffer b = new StringBuffer("12345"); // 創立文字
b.append("6789"); // 加入文字
System.out.println(b);
b.delete(2, 4); // 刪除文字 (開始,結束)
System.out.println(b);
b.insert(3, "34"); // 增加文字 (開始,插入值)
System.out.println(b);
b.replace(1, 5, "二三四"); // 取代(開始,結束,值) 只會取代開始與結束中間的值
System.out.println(b);
b.reverse(); // 顛倒
System.out.println(b);
b.reverse(); // 顛倒
  
String a = new String(b);
String c = a.replaceAll("二三四", "234"); // 正規法取代
System.out.println(c);

執行結果:
1256789
125346789
1二三四6789
9876四三二1
12346789

2.規則表示法

例1:
String s = "123456789";
System.out.println(s.matches("[0-9]")); // false (只比對一位數 s="1"才會是true)
System.out.println(s.matches("[0-9]+")); // ture (+至少出現1次以上的數字)
System.out.println(s.matches("[0-9]*")); // ture (*0或多次)
System.out.println(s.matches("[0-9]{9}")); // ture (剛好出現9個)
System.out.println(s.matches("[0-9]{3,}")); // ture (3個以上)
System.out.println(s.matches("[0-9]{3,8}")); // false (3-8個)

String b = "123";
System.out.println(b.matches("1[^123]3")); // false (^是不包含)
System.out.println(b.matches("1[123]3")); // true
System.out.println(b.matches("1([123])+3")); // true ()為群組
// 手機號碼檢查 String b = "0935751111"; System.out.println(b.matches("0[0-9]{9}")); // true // Eamil String b = "leeeee@yahoo.com.tw"; String b = "leeeee@yahoo.com.tw"; System.out.println(b.matches("[A-Za-z_]{1}[A-Za-z1-9_\\.]*@[A-Za-z1-9_]{1}[A-Za-z1-9_\\.]*")); // true // 簡易身分證 String b = "C222990912"; System.out.println(b.matches("[A-Z]{1}[12]{1}[0-9]{8}")); // true

3.split (explode)

String a = "1,2,3,4,5";
String[] c = a.split(",");
for(String i:c){
 System.out.print(i); // 12345
}

2013年9月12日 星期四

【職訓局-手機程式開發班】2013 9/11 java

CLASS ex1:
class aa{
public int x, y;
public bb b;

aa(int x, int y){
this.x = x;
this.y = y;
b = new bb(this.x, this.y);
}

public String area(){
return  "x:" + this.x + ",y:" + this.y + ",面積為:" + b.area();
}

public String perimeter(){
return "x:" + this.x + ",y:" + this.y + ",邊長為:" + b.perimeter();
}

}

class bb{
public int x, y;
public cc c;

bb(int x, int y){
this.x = x;
this.y = y;
c = new cc(this.x, this.y);
}

public int area(){
return this.x * this.y;
}

public int perimeter(){
return c.perimeter();
}
}

class cc{
public int x, y;

cc(int x, int y){
this.x = x;
this.y = y;
}

public int perimeter(){
return 2 * (this.x + this.y);
}
}


public class test01 {

public static void main(String[] args) {
aa a = new aa(10,20);
a.b.x = 100;
System.out.println(a.area());
a.b.c.x = 1000;
System.out.println(a.perimeter());
}

}
CLASS ex2:
package ch09;

class aa{
public int x, y;
public bb b;

aa(int x, int y){
this.x = x;
this.y = y;
b = new bb(this.x, this.y);
}

public String area(){
return  "x:" + this.x + ",y:" + this.y + ",面積為:" + b.area();
}

public String perimeter(){
return "x:" + this.x + ",y:" + this.y + ",邊長為:" + b.perimeter();
}

}

class bb{
public int x, y;
public cc c;

bb(int x, int y){
this.x = x;
this.y = y;
c = new cc(this.x, this.y);
}

public int area(){
return this.x * this.y;
}

public int perimeter(){
return c.perimeter();
}
}

class cc{
public int x, y;

cc(int x, int y){
this.x = x;
this.y = y;
}

public int perimeter(){
return 2 * (this.x + this.y);
}
}


public class test01 {

public static void main(String[] args) {
aa a = new aa(10,20);
a.b.x = 100;
System.out.println(a.area());
a.b.c.x = 1000;
System.out.println(a.perimeter());
}


}

2013年9月5日 星期四

【職訓局-手機程式開發班】2013 9/4 Java

1.參照
package test;

class A{
 int aa = 10;
}


class B{
 void c(A a){ // 可以傳類別近來
  a.aa = 3;
 }
}

public class class_text {

 public static void main(String[] args) {
  A a = new A();
  B b = new B();
  
  b.c(a);
  
  System.out.println(a.aa); // a.aa = 3
  
 }

}
2.呼叫自己的function
package test;

public class class_test2 {

 int i;
 
 public static void main(String[] args) {
  
  class_test2 c = new class_test2(); // 自己
  c.i = 10;
  c.A();
  
 }
 
 void A(){
  System.out.println(i);
 }

}

2013年8月15日 星期四

【職訓局-手機程式開發班】2013 8/14 Java

1.字串參照特性

String a1 = new String("1234");
String a2 = 1234;
String a3 = 1234;
a1 == a2 -> false
a2 == a3 -> ture
但陣列不適用

2.foreach

package test;

public class foreach {

public static void main(String[] args) {
int[][] $a= {{1,2,3,4},{5,6,7,8}};
int $key1=0, $key2=0;

for(int[] $i:$a){// 注意因為是二維所以第一維還是陣列要用[]
$key1++;
System.out.println($key1 + "{");
for(int $j:$i){
$key2++;
System.out.println($key2 + ":" + $j);
}
System.out.println("}");
System.out.println();
}

}

}

3.argv

import java.util.Arrays;

public class argv {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Arrays.toString(args));
}

}

dos 打 
->javac argv.java
->java argv aa bb cc


2013年8月6日 星期二

【職訓局-手機程式開發班】2013 8/6 Java

基本語法
7. 試寫一程式,可繪製出菱形  要畫多高的星號菱形(若輸入偶數,將自動少一行):7
和解答不一樣的寫法

input = 7;
int t = 0 ,c = 0;
for (int j=1; j<=input*2; j+=2){


if (input >= j){
t = (input - j) / 2;
}else{
t = (j - input) / 2;
}

for (int i=1; i<=t; i++){
System.out.print(" ");
}

c = input-(t*2);
for (int k=1; k<=c ;k++){
System.out.print("*");
}
System.out.println("");

}

2013年7月24日 星期三

【職訓局-手機程式開發班】2013 7/24 Java

當灌好Windows Builder時,物件不能隨意拉要點選Pnae右鍵Set layout->Absolute layout

變數型別
1.基本型別
2.參照型別 - 字串(Stringbuilder)、陣列(Array)、物件(Object)

public class test1 {

public static void main(String[] args) {
int[] myArray1 = {1,2,3,4};
int[] myArray2 = myArray1;
myArray2[0] = 5;
if (myArray1 == myArray2) {
System.out.println(myArray1[0]);
System.out.println(myArray2[0]);
}else{
System.out.println("2");
}
}

}
這比較特別跟之前學的不太一樣


匯入檔案
JAVA
類別只能單一繼承
介面可以多重繼承

常數是在變數那加final 如:final String str = 'aa';


** 與PHP不同 !i只能用在boolean **
boolean i = false;
if (!i){
System.out.println("nun");
}

2013年7月23日 星期二

【職訓局-手機程式開發班】2013 7/23 Java

SDK(Software Develop Kit,軟件開發工具包):用於幫助開發人員的提高工作效率。各種不同類型的軟件開發,都可以有自己的 SDK。Windows 有 Windows SDK,DirectX 有 DirectX 9 SDK,.NET開發也有 Microsoft .NET Framework SDK。JAVA 開發也不含糊,也有自己的 Java SDK。

Java SDK(JDK):最早叫 Java Software Develop Kit,後來改名為 JDK,即 Java Develop Kit。JDK 作為 Java 開發工具包,主要用於構建在 Java 平台上運行的應用程序、Applet 和組件等。

JRE(Java Runtime Environment,Java 運行環境):也就是 Java 平台。所有的 Java 程序都要在 JRE 下才能運行。JDK 的工具也是 Java 程序,也需要 JRE 才能運行。為了保持 JDK 的獨立性和完整性,在 JDK 的安裝過程中,JRE 也是安裝的一部分。所以,在 JDK 的安裝目錄下有一個名為 jre 的目錄,用於存放 JRE 文件。

JVM(Java Virtual Machine,Java 虛擬機):是 JRE 的一部分。它是一個虛構出來的計算機,是通過在實際的計算機上仿真模擬各種計算機功能來實現的。JVM 有自己完善的硬件架構,如處理器、堆棧、寄存器等,還具有相應的指令系統。Java 語言最重要的特點就是跨平台運行。使用 JVM 就是為了支援與操作系統無關,實現跨平台。


一、安裝JDK
1.到網站下載最新版本JDK,安裝到電腦,要對應電腦的版本及位元數。

2.修改環境變數
在我的電腦按右鍵內容->進階系統設定->環境變數
在使用者變數或系統變數(須重開機)那新增
變數名稱:Path
變數值:C:\Program Files\Java\jdk1.7.0_25\bin;(安裝路徑下的bin)
安裝後執行cmd輸入java看看有沒有成功

二、寫程式
1.在C槽下開個java目錄
2.開啟純文字檔,檔名為FirstJava.java
3.輸入第一個程式,class名稱要和檔名一樣。
public class FirstJava {
 public static void main(String[] argv){
  System.out.println("這是我的第一個JAVA程式。");
 }
}
4.進入DOS先編譯程式,先到程式的工作目錄,
輸入javac FirstJava.java
如果沒出現任何東西就代表成功,
會多一個.class這是Byte Code,
再輸入java FirstJava
就會出現要print的字

發現沒有權限的處理方式












三、使用eclipse編輯程式
開啟eclipse後關閉歡迎頁面,就可以使用,
開時要先新增專案在File->New->Java Project,
在到SRC點右鍵新增Class就可以打Java程式,
Ctrl + Shift + F可以自動排版
Ctrl + / 註解
System.out.println(); 快捷鍵sysout Alt + /

如果eclipse文字太小要到螢幕解析度文字的部分做調整


*九九乘法
import java.util.Scanner;
public class test1 {
 public static void main(String[] argv){
  Scanner scanner = new Scanner(System.in);
  System.out.println("請輸入第一個數字");
  int vi = scanner.nextInt();
  System.out.println("請輸入第二個數字");
  int vj = scanner.nextInt();

  //int vi = 9;
  //int vj = 9;
  for (int i=1;i<=vi;i++){
   for(int j=1;j<=vj;j++){
    System.out.print(i + "x" + j + "=" + i*j + " ");
   }
   System.out.print("\n");
  }
 }
}

*取亂數
import java.lang.Math;
public class rand {
 public static void main(String[] avgv){
  int a[];
  a = new int[11];
  double r;

  for (int j=0;j<=10;j++){ 
   a[j] = (int)(Math.random()*100);
   System.out.println(a[j]);
  }

 }
}

四、視窗介面
下載連結 http://download.eclipse.org/windowbuilder/WB/integration/4.2/
字體:
import java.awt.Font;

lblNewLabel.setFont(new Font("微軟正黑體",0,20));


 請撰寫一個程式,顯示單引號(')的標準萬國碼

public static void main(String[] args) {
char x = '銘';
int i = x;
System.out.println("字元:" + x + "的萬國碼是:" + i);

char y = 36084;
System.out.println(y);
}