Monthly Archives: 2月 2016

Java学習–IPアドレスランダム作成をする方法

コード下記
private static String getRndIpAddress() {

StringBuilder sb = null;

int mdda = (int) ((Math.random()*100)*2.24);
int mddb = (int) ((Math.random()*100)*2.55);
int mddc = (int) ((Math.random()*100)*2.55);
int mddd = (int) ((Math.random()*100)*2.55);

String ipAA = Integer.toString(mdda);
String ipBB = Integer.toString(mddb);
String ipCC = Integer.toString(mddc);
String ipDD = Integer.toString(mddd);

sb = new StringBuilder();
sb.append(ipAA).append(“.”).append(ipBB).append(“.”).append(ipCC).append(“.”).append(ipDD);

return sb.toString();

}

jQuery入門–empty()の使い方

コード下記
<!DOCTYPE html>
<html>
<head>
<script src=”http://xxx/jquery/1.10.2/jquery.min.js”>
</script>
<script>
$(document).ready(function(){
$(“button”).click(function(){
$(“#div_empty”).empty();
});
});
</script>
</head>
<body>

<div id=”div_empty” style=”height:120px;width:150px;border:2px solid black;background-color:blue;”>

This is some text in the div.
<p>This is a paragraph in the div.</p>
<p>This is another paragraph in the div.</p>

</div>
<br>
<button>Empty the div element</button>

</body>
</html>

jQuery入門–remove()の使い方

コード下記
<!DOCTYPE html>
<html>
<head>
<script src=”http://xxx/jquery/1.10.2/jquery.min.js”>
</script>
<script>
$(document).ready(function(){
$(“button”).click(function(){
$(“#div_remove”).remove();
});
});
</script>
</head>
<body>

<div id=”div_remove” style=”height:50px;width:260px;border:1px solid black;background-color:yellow;”>

This is some text in the div.
<p>This is a paragraph in the div.</p>
<p>This is another paragraph in the div.</p>

</div>
<br>
<button>Remove div element</button>

</body>
</html>

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″);

Python入門–Datetime、TimeStamp、UTC、localtimeの相互変換する方法

1.TimeStampをDatetimeに変換する
def timestamp2datetime(timestamp, convert_to_local=False):
”’ Converts UNIX timestamp to a datetime object. ”’
if isinstance(timestamp, (int, long, float)):
dt = datetime.datetime.utcfromtimestamp(timestamp)
if convert_to_local:
dt = dt + datetime.timedelta(hours=8)
return dt
return timestamp

2.DatetimeをTimeStampに変換する
def datetime2timestamp(dt, convert_to_utc=False):
”’ Converts a datetime object to UNIX timestamp in milliseconds. ”’
if isinstance(dt, datetime.datetime):
if convert_to_utc:
dt = dt + datetime.timedelta(hours=-8)
timestamp = total_seconds(dt – EPOCH)
return long(timestamp)
return dt
3.UTC時間のTimeStamp
def timestamp_utc_now():
return datetime2timestamp(datetime.datetime.utcnow())

4.local時間のTimeStamp
def timestamp_now():
return datetime2timestamp(datetime.datetime.now())

5.UTC時間をローカル時間に変換する
# python-dateutilのインストール
# Ubuntu下:sudo apt-get install python-dateutil
#PIP:sudo pip install python-dateutil
from dateutil import tz
from dateutil.tz import tzlocal
from datetime import datetime

# get local time zone name
print datetime.now(tzlocal()).tzname()

# UTC Zone
from_zone = tz.gettz(‘UTC’)
# China Zone
to_zone = tz.gettz(‘CST’)

utc = datetime.utcnow()

# Tell the datetime object that it’s in UTC time zone
utc = utc.replace(tzinfo=from_zone)

# Convert time zone
local = utc.astimezone(to_zone)
print datetime.strftime(local, “%Y-%m-%d %H:%M:%S”)

Python入門–プログラム実行時間を計測する方法

コード下記
import time
def start_sleep():
time.sleep(3)
if __name__ == ‘__main__’:
#The start time
start = time.clock()
#A program which will run for 3 seconds
start_sleep()
#The End time
end = time.clock()
print(“The function run time is : %.03f seconds” %(end-start))
# End

Python入門–日付(datetime)の基本的な使い方

コード下記:

import datetime, calendar

lastFriday = datetime.date.today( )
oneday = datetime.timedelta(days=1)
lastFriday -= oneday
while lastFriday.weekday( ) != calendar.FRIDAY:
lastFriday -= oneday
print lastFriday.strftime(‘%A, %d-%b-%Y’)

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);
}
}

1 / 3123