Skip to content

Instantly share code, notes, and snippets.

@BalicantaYao
Created April 25, 2017 14:42
Show Gist options
  • Save BalicantaYao/22dc3f4dd4f6d4975701e4736473ad4c to your computer and use it in GitHub Desktop.
Save BalicantaYao/22dc3f4dd4f6d4975701e4736473ad4c to your computer and use it in GitHub Desktop.
日期取出、列印練習
/*
u1-1. (*) 寫三個方法取出年、月、日 , 2017/01/21
public int getYear(String date){
// 2017
}
public int getMonth(String date){
//1
}
public int getDay(String date){
//21
}
public void printDate(int year, int month, int day){
}
*/
@yaojason
Copy link

yaojason commented Apr 26, 2017

`public class DateExctract {
public int getYear(String data) {
String stryeaar = data.substring(0,4);
int Intyear = Integer.parseInt(stryeaar);
return Intyear;
}
public int getMonth(String data) {
String strmonth = data.substring(5,6);
int Intmonth = Integer.parseInt(strmonth);
return Intmonth;
}
public int getDay(String data) {
String strday = data.substring(7,8);
int Intday = Integer.parseInt(strday);
return Intday;
}
public void printData(int year,int month,int day){
System.out.println(year+"/"+month+"/"+day);
}

public static void main(String[] args) {
String strdata = "20170416";
DateExctract Datamethod = new DateExctract();
int year = Datamethod.getYear(strdata);
int month = Datamethod.getMonth(strdata);
int day = Datamethod.getDay(strdata);
Datamethod.printData(year,month,day);
}
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment