人気ブログランキング | 話題のタグを見る
ブログトップ

智信の部屋

tomonobu.exblog.jp

プログラムに関する雑談なんかを書いていこうと思います。(android記事のまとめサイトはこちらです→http://tomonobu.rocketserver.jp/android_blog_index.php)

Android:【Utility】時間文字列の比較と演算

店舗の開店時間や閉店時間を扱うとなると、
日付情報がなく"HH:MM"といった形式での文字列で扱うことが多いと思う。
っとそうなったとき、確かに既存の Dateクラス を使用するのもいいが、
その場合、いろいろと変換処理なども含めて大変だと思う。
っということでそんなこんなを含めて処理をまとめた時間管理のUtilityを作っておいてみてはどうだろう?
っと思いサンプルとして作ったのがこんなプログラムです。

 "HH:MM"形式の時刻文字列に対する
  ・二つの時刻の比較
   …int timeStrigComparison(String src_time, String dst_time)
    TIME_ERROR:エラー
    TIME_SRC_MIN:src_time < dst_time
    TIME_EQUAL:src_time == dst_time
    TIME_SRC_MAX :src_time > dst_time
    ※(TIME_SRC_MIN | TIME_EQUAL)などを使用して&比較することで以下なども判断可能
  ・時刻の加算処理
   …String timeStringAddTimeString(String src_time, String add_time, boolean limit_24)
   …String timeStringAddTimeString(String src_time, String add_time)
    ※limit_24は表記を23:59までとするかどうかの設定
  ・時刻の減算処理
   …String timeStringSubTimeString(String src_time, String sub_time)

基本動作は確認してありますがバグがあるかもしれないので
使用するときは十分テストしてくださいね!!

Utiliryコード
public class Utility {
public static int TIME_ERROR = 0x00;
public static int TIME_EQUAL = 0x01;
public static int TIME_SRC_MIN = 0x02;
public static int TIME_SRC_MAX = 0x04;

public static int timeStrigComparison(String src_time, String dst_time) {
String src_times[] = src_time.split(":");
String dst_times[] = dst_time.split(":");

if (src_times.length != 2 || dst_times.length != 2) {
return TIME_ERROR;
}
try {
int src_value = Integer.valueOf(src_times[0]) * 100 + Integer.valueOf(src_times[1]);
int dst_value = Integer.valueOf(dst_times[0]) * 100 + Integer.valueOf(dst_times[1]);

if (src_value < dst_value) {
return TIME_SRC_MIN;
}
else if (src_value > dst_value) {
return TIME_SRC_MAX;
}
return TIME_EQUAL;
}
catch (NumberFormatException e) {
return TIME_ERROR;
}
}

public static String timeStringAddTimeString(String src_time, String add_time, boolean limit_24) {
String src_times[] = src_time.split(":");
String add_times[] = add_time.split(":");

if (src_times.length != 2 || add_times.length != 2) {
return null;
}
try {
int src_h_value = Integer.valueOf(src_times[0]);
int src_m_value = Integer.valueOf(src_times[1]);
int add_h_value = Integer.valueOf(add_times[0]);
int add_m_value = Integer.valueOf(add_times[1]);

int dst_m_value = src_m_value + add_m_value;
int dst_h_value = src_h_value + add_h_value + (dst_m_value / 60);
dst_m_value %= 60;
if (limit_24) {
dst_h_value %=24;
}
if (dst_m_value < 10) {
return (String)(dst_h_value + ":0" + dst_m_value);
}
else {
return (String)(dst_h_value + ":" + dst_m_value);
}
}
catch (NumberFormatException e) {
return null;
}
}

public static String timeStringAddTimeString(String src_time, String add_time) {
return timeStringAddTimeString(src_time, add_time, false);
}

public static String timeStringSubTimeString(String src_time, String sub_time) {
String src_times[] = src_time.split(":");
String sub_times[] = sub_time.split(":");

if (src_times.length != 2 || sub_times.length != 2) {
return null;
}
try {
int src_h_value = Integer.valueOf(src_times[0]);
int src_m_value = Integer.valueOf(src_times[1]);
int sub_h_value = Integer.valueOf(sub_times[0]);
int sub_m_value = Integer.valueOf(sub_times[1]);

int dst_m_value = src_m_value - sub_m_value;
int dst_h_value = src_h_value - sub_h_value + (dst_m_value / 60);
dst_m_value %= 60;
if (dst_m_value < 0) {
dst_h_value --;
dst_m_value += 60;
}
if (dst_h_value < 0) {
dst_h_value %= 24;
if (dst_h_value < 0) {
dst_h_value += 24;
}
}
if (dst_m_value < 10) {
return (String)(dst_h_value + ":0" + dst_m_value);
}
else {
return (String)(dst_h_value + ":" + dst_m_value);
}
}
catch (NumberFormatException e) {
return null;
}
}
}


by Tomonobu1979 | 2011-07-26 01:46 | android

by Tomonobu1979