「Java入門」 java.lang.String.lastIndexOf(String str)のサンプル

Javaコード

package itstudy;

public class SearchlastString {

public static void main(String[] args) {
String strOrg = “Hello world ,Hello Reader,Hello Tony”;
int lastIndex = strOrg.lastIndexOf(“Hello”);

if (lastIndex == -1) {
System.out.println(“Hello not found”);
} else {
System.out.println(“Last occurrence of Hello is at index ” + lastIndex);
}
}

}

結果:Last occurrence of Hello is at index 26

「MySQL入門」str_to_date()で文字列からdatetime型にするコマンド

サンプル:
SELECT
str_to_date(
concat (left(date_str,10) , right(date_str,10)),
‘%Y/%m/%d %H:%i:%s’ ) as cft FROM xxx;

「Java」java.text.SimpleDateFormatクラスの使い方

Javaコード:
import java.util.Date;
import java.text.DateFormat;

public class DateDemo{
public static void main(String []args){
Date d = new Date();
String s;

System.out.println(d);

System.out.println(“******************************************”);

/** getDateInstance() */
s = DateFormat.getDateInstance().format(d);
System.out.println(s);

s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
System.out.println(s);

s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
System.out.println(s);

s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
System.out.println(s);

s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);
System.out.println(s);

 

java.text.DateFormat fmt = new java.text.SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
s = fmt.format(new Date());
System.out.println(s);

System.out.println((new java.text.SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”)).format(new Date()));

java.text.DateFormat cft = new java.text.SimpleDateFormat(“yyyyMMddhhmmss”);
s = cft.format(new Date());
System.out.println(s);
}
}

「java入門」複数ファイルを Zip に圧縮するサンプル

Javaコード
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outfilePath));
byte[] buffer = new byte[1024];
int len;

for(String file : files) {
InputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();

}
out.close();

} catch(FileNotFoundException e){

} catch(Exception e){

}

「java」org.apache.commons.beanutilsでmapをobjectに変換するサンプル

javaコード
class DemoCft {

public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null;

Object obj = beanClass.newInstance();

org.apache.commons.beanutils.BeanUtils.populate(obj, map);

return obj;
}

public static Map<?, ?> objectToMap(Object obj) {
if(obj == null)
return null;

return new org.apache.commons.beanutils.BeanMap(obj);
}

}

「java」IntrospectorでObject型からMapへキャストするサンプル

Javaコード:
class IntrospectorDemo {

public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null;

Object obj = beanClass.newInstance();

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (setter != null) {
setter.invoke(obj, map.get(property.getName()));
}
}

return obj;
}

public static Map<String, Object> objectToMap(Object obj) throws Exception {
if(obj == null)
return null;

Map<String, Object> map = new HashMap<String, Object>();

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase(“class”) == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter!=null ? getter.invoke(obj) : null;
map.put(key, value);
}

return map;
}

}

「Oracle入門」階層問い合わせ(start with connect by)をするサンプル

サンプル
select *
from tableName t
start with t.columnName_A = ‘value’
connect by prior t.columnName_B = t.columnName_A

「Java入門」ZipOutputStreamでファイルをzipに圧縮するコード

javaコード
private void FiletoZipFunc(List<File> fileList) throws IOException{
if(null != fileList && !fileList.isEmpty()){
byte[] buf = new byte[1024];
String url = REAL_PATH + “/” + EXPORT_URL + “/” + TABLE_NAME;
String fi = url + “/” + TABLE_NAME + “.zip”;
FileOutputStream os = new FileOutputStream(fi);
ZipOutputStream zipOut = new ZipOutputStream(os);
for(File file : fileList){
FileInputStream in = new FileInputStream(file);
zipOut.putNextEntry(new ZipEntry(file.getName()));
int len;
while((len = in.read(buf)) > 0){
zipOut.write(buf, 0, len);
}
zipOut.closeEntry();
in.close();
file.delete();//ファイル削除
}
zipOut.close();
}
}

JavaScriptで整数を判定するコード

サンプルコード:
var type=”^[0-9]*[1-9][0-9]*$”;
var redd = new RegExp(type);
if(subjectHour.value.match(redd)==null) {
alert(“整数を入力してください”);
return false;
}

「jquery入門」.hide()で要素を非表示する

htmlコード
<body>
<button>click</button>
<p>test message data</p>
</body>

方法.hide()
$(function() {
$(‘button’).click(function(){
$(‘p’).hide();
});
});

方法css(‘display’, ‘none’)
$(function() {
$(‘button’).click(function(){
$(‘p’).css(‘display’, ‘none’);
});
});