[an error occurred while processing this directive]
[an error occurred while processing this directive]
Java の日時
Java の File.lastModified() 等で取れる日時は、epoch (1970/1/1) からの
ミリ秒値を 64bit signed int であらわしたものです。
なので、C の gettimeofday() 等で取得できる struct timeval と比較すると、
usec 部分を 1000 で割って切り捨てる必要があります。
下の二つのコードは同じ値を返します。
public class TvTest {
public static void main(String[] args){
long l = System.currentTimeMillis() ;
System.out.println("lastModofied: " + Long.toHexString(l));
}
}
#include <sys/time.h>
int main(void){
struct timeval tv;
long long int jtv;
gettimeofday(&tv, 0);
jtv = tv.tv_sec * 1000;
jtv += (tv.tv_usec / 1000);
printf("Java style: %16llx\n", jtv);
}
入れ子クラスを指定する変数
内部クラス(ネストしたクラス)の親(外側)を指定するときには、親クラス.this を用いる。
class Oya {
class Kodomo {
void f(){
Oya.this;
}
}
}
java で html エスケープ
Commons.apache.org の StringEscapeUtils を使うとできるらしい。
即席HSVマップ出力プログラム
たいして見所はなんんだけど、あえて言うなら
16進数で桁数を揃えた表示をするために、シフトとsubStringを組み合わせてます。
例えば、0<= i<256なiを0aとかffとか表示したい時は、
String str = Integer.toHexString(0x100 + i);
str.subString(1);
とします。javaにはsprintfがないし、DecimalFormatは"Decimal"だから10進数しか使えないし。
import java.io.*;
import java.awt.*;
public class Col {
static String getColStr(float r, float g, float b){
String str = Integer.toHexString((1<<24) + ((int)r <<16) + ((int)g<<8) + (int)b);
return str.substring(1);
}
public static void main(String[] args){
System.out.println("<html><body>");
for(float s = 0.0f; s <= 1.0f; s += 0.2f){
System.out.println("s=" + s + "<table><tr>");
for(float v = 0.2f; v <= 1.0f; v += 0.2f){
System.out.println("<tr>");
for(float h = 0.0f; h < 1.0f; h += 0.1f){
Color cl = Color.getHSBColor(h, s, v);
String str = getColStr(cl.getRed(), cl.getGreen(), cl.getBlue());
System.out.println("<td style='width:60px;background-color:#" + str + "'>"+ str + "</td>");
}
System.out.println("</tr>");
}
System.out.println("</table><br />");
}
System.out.println("</body></html>");
}
}
出力したHSVマップ
Java1.5でMapの周辺
import java.io.*;
import java.util.*;
public class Te{
public static void main(String[] args){
Map<String, Integer> hm = new HashMap<String, Integer>();
hm.put("kei", 10);
hm.put("mei", 2);
hm.put("rei", 8);
Set<Map.Entry<String, Integer>> s = hm.entrySet();
for(Map.Entry<String, Integer> e : s){
System.out.println("key \t: " + e.getKey());
System.out.println("value \t: " + e.getValue());
}
SortedSet<Map.Entry<String, Integer>> ss
= new TreeSet<Map.Entry<String, Integer>>
(new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String, Integer> e0, Map.Entry<String, Integer> e1)
{return(e0.getValue() - e1.getValue()); }
public boolean equals (Object o) {return (super.equals(o));}
});
s.addAll(s);
for(Map.Entry<String, Integer> e : ss){
System.out.println("key \t: " + e.getKey());
System.out.println("value \t: " + e.getValue());
}
}
}
key : rei
value : 8
key : mei
value : 2
key : kei
value : 10
[an error occurred while processing this directive]