解壓縮 tar.gz
tar -zxvf xxxx.tar.gz
壓縮 tar.gz
tar -zcvf xxxx.tar.gz /folder
解壓縮 tar.bz2
tar -jxvf xxxx.tar.bz2
壓縮 tar.bz2
tar -jcvf xxxx.tar.bz2 /folder
解開 .tar
tar -xvf xxxx.tar
打包 .tar
tar -cvf xxxx.tar /folder
2013年8月14日 星期三
2013年7月3日 星期三
日期相減取得秒數
方法說明:
isHoliday() 方法判斷傳入日期是否為六日
getDuration() 方法回傳相減過後的秒數,包含六日
getTimeFrame() 方法回傳相減過後的秒數,不包含六日
/**
* Check holiday
*
* @param startTime
* @param endTime
* @return true or false
*/
public static boolean isHoliday(Calendar
calander) {
boolean isHoliday = false;
if
(calander.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
&& calander.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
isHoliday =
false;
} else {
isHoliday =
true;
}
return isHoliday;
}
/**
* Get the total
seconds between the startTime & endTime
*
* @param startTime
* @param endTime
* @return
*/
public static long getDuration(Date
startTime, Date endTime) {
long totalSeconds = 0;
Calendar
startCal = Calendar.getInstance();
startCal.setTime(startTime);
Calendar endCal
= Calendar.getInstance();
endCal.setTime(endTime);
totalSeconds =
(endCal.getTimeInMillis() - startCal.getTimeInMillis()) / 1000;
return totalSeconds;
}
/**
* Get the total
seconds between the startTime & endTime (Without Sunday &
* Saturday)
*
* @param startTime
* @param endTime
* @return
*/
public static long getTimeFrame(Date
startTime, Date endTime) {
long timeFrame = 0;
Calendar
startCal = Calendar.getInstance();
startCal.setTime(startTime);
//startCal.add(Calendar.DATE, -6);
Calendar endCal
= Calendar.getInstance();
endCal.setTime(endTime);
//endCal.add(Calendar.DATE, 1);
int useDays = 0;
long totalSeconds = 0;
Calendar
tmpDate = Calendar.getInstance();
tmpDate.setTimeInMillis(startCal.getTimeInMillis());
if
(DateUtils.isSameDate(startCal, endCal) && startCal.before(endCal)) {
useDays =
0;
totalSeconds = (endCal.getTimeInMillis() - startCal.getTimeInMillis()) /
1000;
return totalSeconds;
} else if
(!DateUtils.isSameDate(startCal, endCal) && startCal.before(endCal)) {
long
secondsPastOfEndTime = endCal.get(Calendar.SECOND) +
endCal.get(Calendar.MINUTE) * 60
+
endCal.get(Calendar.HOUR_OF_DAY) * 60 * 60;
long
secondsRemainOfStartTime = 0;
while (tmpDate.before(endCal))
{
tmpDate.add(Calendar.DATE, 1);
if
(DateUtils.isSameDate(tmpDate, endCal)) {
break;
} else {
if
(!MxDateUtility.isHoliday(startCal) && MxDateUtility.isHoliday(endCal))
{
useDays++;
} else {
if (tmpDate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
&&
tmpDate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
useDays++;
}
}
}
}
if
(MxDateUtility.isHoliday(startCal) && !MxDateUtility.isHoliday(endCal))
{
secondsRemainOfStartTime = 0;
} else {
secondsRemainOfStartTime = 86400 - (startCal.get(Calendar.SECOND) +
startCal.get(Calendar.MINUTE) * 60 + startCal
.get(Calendar.HOUR_OF_DAY) * 60 * 60);
}
long secondsOfUseDays =
useDays * 86400;
totalSeconds
= secondsRemainOfStartTime + secondsOfUseDays + secondsPastOfEndTime;
return totalSeconds;
} else {
return 0;
}
}
2013年5月17日 星期五
List 排序
使用 Collections.sort(list) 方法
本方法只能針對 List<Integer> 及 List<String> 進行『升冪排序』
詳見<範例程式 1>及 <範例程式 2>
若要針對其它自訂 Object 物件進行排序
則必須將自訂 Object 宣告為實作 Comparator 之物件,並於 Class 實作 Compare() 方法
實作時可自定排序方式為『升冪』或『降冪』
詳見<範例程式 3>
本例子為升冪,修改 compare() 方法之第一個 if 判斷為
Long.parseLong(value2) - Long.parseLong(value1) > 0
即可轉為降冪排序
--------------範例程式 1--------------
public static void main(String[] args) {
範例程式 1 執行結果 :
1
2
3
4
--------------範例程式 2--------------
public static void main(String[] args) {
範例程式 2 執行結果 :
a
b
c
d
--------------範例程式 3--------------
<3-1>
import java.util.Comparator;
範例程式 3 執行結果 :
本方法只能針對 List<Integer> 及 List<String> 進行『升冪排序』
詳見<範例程式 1>及 <範例程式 2>
若要針對其它自訂 Object 物件進行排序
則必須將自訂 Object 宣告為實作 Comparator 之物件,並於 Class 實作 Compare() 方法
實作時可自定排序方式為『升冪』或『降冪』
詳見<範例程式 3>
本例子為升冪,修改 compare() 方法之第一個 if 判斷為
Long.parseLong(value2) - Long.parseLong(value1) > 0
即可轉為降冪排序
--------------範例程式 1--------------
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(4);
list.add(1);
list.add(3);
list.add(2);
Collections.sort(list);
for (int value : list) {
System.out.println(value);
}
}
範例程式 1 執行結果 :
1
2
3
4
--------------範例程式 2--------------
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("d");
list.add("a");
list.add("c");
list.add("b");
Collections.sort(list);
for (String value : list) {
System.out.println(value);
}
}
範例程式 2 執行結果 :
a
b
c
d
--------------範例程式 3--------------
<3-1>
import java.util.Comparator;
public class TestBean implements Comparator
{
String modifyDate;
double value;
public TestBean() {
}
public TestBean(String
lastModifyDate, double averageTestValue) {
this.modifyDate = lastModifyDate;
this.value =
averageTestValue;
}
@Override
public int compare(Object
obj1, Object obj2) {
TestBean
bean1 = (TestBean) obj1;
TestBean
bean2 = (TestBean) obj2;
String
value1 = bean1.getModifyDate();
String
value2 = bean2.getModifyDate();
if (Long.parseLong(value1)
- Long.parseLong(value2) > 0) {
return 1;
} else if
(value1.equals(value2)) {
return 0;
} else {
return -1;
}
}
public String
getModifyDate() {
return modifyDate;
}
public void
setModifyDate(String modifyDate) {
this.modifyDate = modifyDate;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
}
<3-2>
import java.util.ArrayList;
import java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
import java.util.List;
public class Test {
public static void main(String[]
args) {
List<TestBean> testList = new ArrayList<TestBean>();
testList.add(new TestBean("20130314000000", 1));
testList.add(new TestBean("20130311000000", 2));
testList.add(new TestBean("20130313000000", 3));
testList.add(new TestBean("20130312130345", 4));
testList.add(new TestBean("20130312110435", 5));
Comparator
comp = new TestBean();
Collections.sort(testList,
comp);
for (TestBean a :
testList) {
System.out.println(a.getModifyDate()
+ ", " + a.getValue());
}
}
}
範例程式 3 執行結果 :
20130311000000, 2.0
20130312110435, 5.0
20130312130345, 4.0
20130313000000, 3.0
20130314000000, 1.0
20130312110435, 5.0
20130312130345, 4.0
20130313000000, 3.0
20130314000000, 1.0
2013年5月6日 星期一
Oracle, SQL 以多個欄位為 Key, 取出 table 中另一欄位為 max 之資料
table_1 資料表如下圖:
如上資料表中,此資料表中並沒有設定 Primary Key欄位,但可由 column_1, column_2, column_3, column_4, column_5, column_6 的組合當成 Key 值,但仍有可能有重覆之資料,如表中所示,column_4 = 8 , 9 , 10 的資料都有多筆,但取資料時又希望依據相同的數據,將這些重覆的過濾掉只留下 check_column 數字最大的那筆時,可利用下面的SQL完成。
select *
from table_1
where (column_1, column_2, column_3, column_4, column_5, column_6, check_column)
in (
select
column_1,
column_2,
column_3,
column_4,
column_5,
column_6,
max(check_column)
from table_1
group by
column_1,
column_2,
column_3,
column_4,
column_5,
column_6
)
結果如下圖:
2013年4月11日 星期四
Java 四捨五入至小數第 n 位
//欲四捨五入的數字
double y = 1.890784;
//設定要到小數第 3 位
int n = 3;
//執行四捨五入
double x = new BigDecimal(y).setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue();
//印出結果
System.out.println(x);
//不要有科學記號出現的方法
double x = new BigDecimal(y).setScale(n, BigDecimal.ROUND_HALF_UP).toPlainString();
double y = 1.890784;
//設定要到小數第 3 位
int n = 3;
//執行四捨五入
double x = new BigDecimal(y).setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue();
//印出結果
System.out.println(x);
//不要有科學記號出現的方法
double x = new BigDecimal(y).setScale(n, BigDecimal.ROUND_HALF_UP).toPlainString();
2013年4月2日 星期二
Java XPath 使用範例
XML 範例:books.xml
執行結果:
name of the Book: Java Programing
Total number of authors for the book Java Programing is --> 3
Author 1: Tuna TORE
Author 2: Linus Torvalds
Author 3: James Gosling
NAME value: Natural Science
CONTENT value: Science
AUTHORS value:
John Green
Bill Gates
參考文件:http://tunatore.wordpress.com/2011/05/20/how-to-use-xpath-i-java-simple-example/
<?xml version="1.0" encoding="UTF-8"?>
<LIBRARY>
<BOOKS>
<BOOK isbn="ABCD7327923">
<NAME>Java Programing</NAME>
<SUBJECT>Java J2EE</SUBJECT>
<AUTHORS>
<AUTHOR>Tuna TORE</AUTHOR>
<AUTHOR>Linus Torvalds</AUTHOR>
<AUTHOR>James Gosling</AUTHOR>
</AUTHORS>
</BOOK>
<BOOK isbn="DFGH09093232">
<NAME>XPATH for Dummies</NAME>
<SUBJECT> XPATH development</SUBJECT>
<AUTHORS>
<AUTHOR>Linus Torvalds</AUTHOR>
<AUTHOR>John Hawking</AUTHOR>
</AUTHORS>
</BOOK>
<BOOK isbn="DSKL2393A">
<NAME>J2EE Patterns</NAME>
<SUBJECT>Design Patterns</SUBJECT>
<AUTHORS>
<AUTHOR>Aka Tuna</AUTHOR>
</AUTHORS>
</BOOK>
</BOOKS>
<DVDS>
<DVD id="123456">
<NAME>Music DVD</NAME>
<CONTENT>Music</CONTENT>
<AUTHORS>
<AUTHOR>James Gosling</AUTHOR>
<AUTHOR>Bill Gates</AUTHOR>
</AUTHORS>
</DVD>
<DVD id="3213324">
<NAME>Natural Science</NAME>
<CONTENT>Science</CONTENT>
<AUTHORS>
<AUTHOR>John Green</AUTHOR>
<AUTHOR>Bill Gates</AUTHOR>
</AUTHORS>
</DVD>
<DVD id="4353534">
<NAME>Rally</NAME>
<CONTENT>Race</CONTENT>
<AUTHORS>
<AUTHOR>Tuna</AUTHOR>
</AUTHORS>
</DVD>
</DVDS>
</LIBRARY>
程式範例: XpathTest.java
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class XPathTest {
public static void main(String[] args) throws Exception {
//loading the XML document from a file
DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
builderfactory.setNamespaceAware(true);
DocumentBuilder builder = builderfactory.newDocumentBuilder();
Document xmlDocument = builder.parse("src\\books.xml");
XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
XPath xPath = factory.newXPath();
//getting the name of the book having an isbn number == ABCD7327923
String isbnNum = "ABCD7327923";
XPathExpression xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" + isbnNum + "']//NAME");
String nameOfTheBook = xPathExpression.evaluate(xmlDocument, XPathConstants.STRING).toString();
System.out.println("name of the Book ---> " + nameOfTheBook);
//getting all authors for the book having an isbn number == ABCD7327923
xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" + isbnNum + "']//AUTHORS//AUTHOR");
NodeList nodeListBook = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
System.out.println("Total number of authors for the book " + nameOfTheBook + " is --> "
+ nodeListBook.getLength());
for (int index = 0; index < nodeListBook.getLength(); index++) {
String author = nodeListBook.item(index).getTextContent();
System.out.println("Author " + (index + 1) + " ---> " + author);
}
//getting all tags for the dvd having id == 3213324
String dvdID = "3213324";
xPathExpression = xPath.compile("//LIBRARY//DVDS//DVD[@id='" + dvdID + "']");
NodeList nodeDVD = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
NodeList nodeListForDVD = nodeDVD.item(0).getChildNodes();
for (int index = 0; index < nodeDVD.item(0).getChildNodes().getLength(); index++) {
String tagName = nodeListForDVD.item(index).getNodeName();
String tagValue = nodeListForDVD.item(index).getTextContent();
if (!tagName.equals("#text")) //handling empty comment (#text)
System.out.println(tagName + " value ---> " + tagValue);
}
}
}
執行結果:
name of the Book: Java Programing
Total number of authors for the book Java Programing is --> 3
Author 1: Tuna TORE
Author 2: Linus Torvalds
Author 3: James Gosling
NAME value: Natural Science
CONTENT value: Science
AUTHORS value:
John Green
Bill Gates
參考文件:http://tunatore.wordpress.com/2011/05/20/how-to-use-xpath-i-java-simple-example/
訂閱:
文章 (Atom)