Category Archives: Java

Javaで週の最初(最後)の日、月のの最初(最後)の日を求める方法

Javaコード:
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
SimpleDateFormat sdfall=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

//日付の開始、終了時間
startDate=sdf.parse(sdf.format(calendar.getTime()));
endDate=sdfall.parse(sdf.format(calendar.getTime())+” 23:59:59″);

//週の最初の日、最後の日
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK,calendar.getFirstDayOfWeek());
startDate=sdf.parse(sdf.format(calendar.getTime()));
calendar.set(Calendar.DAY_OF_WEEK,calendar.getFirstDayOfWeek()+6);
endDate=sdfall.parse(sdf.format(calendar.getTime())+” 23:59:59″);

//当月の最初の日、最後の日
calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), 1);
startDate=sdf.parse(sdf.format(calendar.getTime()));
calendar.roll(Calendar.DATE, -1);
endDate=sdfall.parse(sdf.format(calendar.getTime())+” 23:59:59″);

Java開発でLog4jのpropertiesの設定方法

設定ファイル:
log4j.rootLogger = DEBUG, CONSOLE,A1
log4j.addivity.org.apache = true

# console
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.Threshold = DEBUG
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n
#log4j.appender.CONSOLE.layout.ConversionPattern = [start] % d {DATE} [DATE] % n % p[PRIORITY] % n % x[NDC] % n % t[THREAD] n % c[CATEGORY] % n % m[MESSAGE] % n % n

#ファイル 
log4j.appender.FILE = org.apache.log4j.FileAppender
log4j.appender.FILE.File = file.log
log4j.appender.FILE.Append = false
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n
# Use this layout for LogFactor 5 analysis

# ファイル ロールバック
log4j.appender.ROLLING_FILE = org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold = ERROR
log4j.appender.ROLLING_FILE.File = rolling.log
log4j.appender.ROLLING_FILE.Append = true
log4j.appender.ROLLING_FILE.MaxFileSize = 100KB
log4j.appender.ROLLING_FILE.MaxBackupIndex = 1
log4j.appender.ROLLING_FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n

#socket
log4j.appender.SOCKET = org.apache.log4j.RollingFileAppender
log4j.appender.SOCKET.RemoteHost = localhost
log4j.appender.SOCKET.Port = 5001
log4j.appender.SOCKET.LocationInfo = true
# Set up for Log Facter 5
log4j.appender.SOCKET.layout = org.apache.log4j.PatternLayout
log4j.appender.SOCET.layout.ConversionPattern = [start] % d {DATE} [DATE] % n % p[PRIORITY] % n % x[NDC] % n % t[THREAD] % n % c[CATEGORY] % n % m[MESSAGE] % n % n

# Log Factor 5 Appender
log4j.appender.LF5_APPENDER = org.apache.log4j.lf5.LF5Appender
log4j.appender.LF5_APPENDER.MaxNumberOfRecords = 2000

#Send log to mail
log4j.appender.MAIL = org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold = FATA
log4j.appender.MAIL.BufferSize = 10
log4j.appender.MAIL.From = from@arkgame.com
log4j.appender.MAIL.SMTPHost = arkgame.com
log4j.appender.MAIL.Subject = Log4J Message
log4j.appender.MAIL.To = to@arkgame.com
log4j.appender.MAIL.layout = org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n

#DataBase
log4j.appender.DATABASE = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL = jdbc:mysql://localhost:3306/ccdb
log4j.appender.DATABASE.driver = com.mysql.jdbc.Driver
log4j.appender.DATABASE.user = root
log4j.appender.DATABASE.password =jz345password
log4j.appender.DATABASE.sql = INSERT INTO LOG4J (Message) VALUES ( ‘ [framework] %d – %c -%-4r [%t] %-5p %c %x – %m%n ‘ )
log4j.appender.DATABASE.layout = org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n
log4j.appender.A1 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File = SampleMessages.log4j
log4j.appender.A1.DatePattern = yyyyMMdd – HH ‘ .log4j ‘
log4j.appender.A1.layout = org.apache.log4j.xml.XMLLayout

#define Appender
log4j.appender.im = net.jz345.util.logger.appender.IMAppender
log4j.appender.im.host = mail.arkgame.com
log4j.appender.im.username = username
log4j.appender.im.password = password
log4j.appender.im.recipient = admin_user@arkgame.com
log4j.appender.im.layout = org.apache.log4j.PatternLayout
log4j.appender.im.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n

Java入門|HashMapとIteratorの使い方

Javaコード:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Studyit {

public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put(“birth_yy”, “1982”);
map.put(“birth_mm”, “09”);
map.put(“birth_dd”, “25”);
System.out.println(“誕生日:”);
for (Iterator iterator = map.values().iterator(); iterator.hasNext();) {
String strBirth = (String) iterator.next();
System.out.println(strBirth);
}
}

Java入門/LinkedHashMapとIteratorの使い方

Javaコード:
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class Studyit {
public static void main(String[] args) {
Map<String,String>map =new LinkedHashMap<String,String>();
map.put(“apply_year”, “2016”);
map.put(“apply_mont”, “02”);
map.put(“apply_day”, “19”);
for (Iterator iterator = map.values().iterator();iterator.hasNext();){
String strTime=(String)iterator.next();
System.out.println(“LinkedHap value :”+strTime);
}
}

Java入門–TreeMapとIteratorを利用するサンプルコード

Javaコード:
package com.startnews24.studyit;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class StudyTreemap {
public static void main(String[] args) {

Map<String,String>map =new TreeMap<String,String>(new Comparator<Object>(){
public int compare(Object obj1,Object obj2){
String key1= obj1.toString();
String key2= obj2.toString();
return key1.compareTo(key2);
}
});
map.put(“1”, “treemap_AA”);
map.put(“2”, “treemap_BB”);
map.put(“3”, “treemap_CC”);
for(Iterator iterator = map.values().iterator();iterator.hasNext();){
String name=(String)iterator.next();
System.out.println(“result:”+name);
}

}

}

Java勉強|文字列の前後の空白を除去する方法

Javaコード:
public static String strTrim(String str){
if(str == null)
return null;
String strpp = new String(“”);
strpp = str.trim();
int i=0;
int j=strpp.length();
for(;i<strpp.length();i++){
if(!strpp.substring(i,i+1).equals(“ ”)){
for(;j>0;j–){
if(!strpp.substring(j-1,j).equals(“ ”)) break;
}
break;
}
}
strpp = strpp.substring(i,j);
return strpp;
}

Java入門-ファイルを作成するサンプルコード

Javaコード:
package com.startnews24.filecreate;

import java.io.File;
import java.io.IOException;

public class FileCreatestartnews24
{
public static void main( String[] args )
{
try {
File file = new File(“D:\\startnews24_file.txt”);
if (file.createNewFile()){
System.out.println(“ファイルの作成が完了”);
}else{
System.out.println(“ファイルがすでに存在する.”);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

JavaでローカルのIPアドレスを取得するサンプルコード

Javaコード:
import java.net.InetAddress;
import java.net.UnknownHostException;

public class IpGetStartnews24Demo {

private String strIp;
public String getlocalip()
{
try {
InetAddress ips=InetAddress.getLocalHost();
String ipstr=ips.toString();
String[] ipstring=ipstr.split(“/”);
s=ipstring[1];
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return strIp;

}

}

JavaでBufferedWriterクラスを利用してファイルに書き込むサンプル

Javaコード
package com.startnews24.fileoper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteDemo {
public static void main(String[] args) {
try {

String content = “start to write content to file”;

File file = new File(“/www/data/startnews24study_file.txt”);
if (!file.exists()) {
file.createNewFile();
}

FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();

System.out.println(“write finished”);

} catch (IOException e) {
e.printStackTrace();
}
}
}

Java9でtry-with-resourcesの使い方

1.リソースの宣言
// A final resource
final Resource resource1 = new Resource(“resource1”);
// An effectively final resource
Resource resource2 = new Resource(“resource2”);

2.Java7とJava8で処理方法
// Original try-with-resources statement from JDK 7 or 8
try (Resource r1 = resource1;
Resource r2 = resource2) {
// Use of resource1 and resource 2 through r1 and r2.
}
3.Java9で処理方法
// New and improved try-with-resources statement in JDK 9
try (resource1;
resource2) {
// Use of resource1 and resource 2.
}