String ke Calendar, Calendar ke String - Java - Publisher - Royz Rambo
Ilham Penta
Site menu
Section categories
My articles [6]
Java [14]
Java Programming
Flash [6]
Flash
URL web dan Tutorial [29]
Petualangan [1]
Our poll
Rate my site
Total of answers: 19
Statistics

Total online: 1
Guests: 1
Users: 0
Login form
Main » Articles » Java

String ke Calendar, Calendar ke String
package com.robohobby.me;
import java.util.Calendar;

public class StrCalendar {
static public java.util.Calendar strToCalendar(String str) {
//For strings, like: 2003-03-18 15:46:53

int y, month, date, h, min, sec;

y = Integer.parseInt(str.substring(0, 4));
month = Integer.parseInt(str.substring(5, 7));
month -= 1;
date = Integer.parseInt(str.substring(8, 10));
h = Integer.parseInt(str.substring(11, 13));
min = Integer.parseInt(str.substring(14, 16));
sec = Integer.parseInt(str.substring(17, 19));

java.util.Calendar c = java.util.Calendar.getInstance();
//c.clear();
//c.set(y,month,date,h,min,sec);
c.set(Calendar.YEAR, y);
c.set(Calendar.MONTH, month);
c.set(Calendar.DATE, date);
c.set(Calendar.HOUR_OF_DAY, h);
c.set(Calendar.MINUTE, min);
c.set(Calendar.SECOND, sec);

return c;

}

/**

* Method for converstion from Java Calendar to Java type String.

* Returns JavaString in 'Java Date/Time format'.

*
* @param c
* @return
*/
static public String calendarToStr(java.util.Calendar c) {
int y = c.get(java.util.Calendar.YEAR);

int m = c.get(java.util.Calendar.MONTH);

m += 1;

String mStr = addZero(m);

int d = c.get(java.util.Calendar.DAY_OF_MONTH);
String dStr = addZero(d);

int h = c.get(java.util.Calendar.HOUR_OF_DAY);
String hStr = addZero(h);

int mn = c.get(java.util.Calendar.MINUTE);
String mnStr = addZero(mn);

int s = c.get(java.util.Calendar.SECOND);
String sStr = addZero(s);

return ("" + y + "-" + mStr + "-" + dStr + " " + hStr + ":" + mnStr + ":" + sStr);
}
/**

* Subversion of method calendartoStr,
* safe for file system.

* Using this method you can get string without symbols ":" and 'spacebar'.

* As a result you can use this string as a file name for saving some info

* in file on a disc, etc.
* Resulting strings should look like: 2009-10-11_11-45-59 and this format is

* 'file-system friendly'.
*
* @param c

* @return Date in Java format
*/
static public String calendarToStrFs(java.util.Calendar c) {
int y = c.get(java.util.Calendar.YEAR);

int m = c.get(java.util.Calendar.MONTH);

m += 1;

String mStr = addZero(m);

int d = c.get(java.util.Calendar.DAY_OF_MONTH);
String dStr = addZero(d);

int h = c.get(java.util.Calendar.HOUR_OF_DAY);
String hStr = addZero(h);

int mn = c.get(java.util.Calendar.MINUTE);
String mnStr = addZero(mn);

int s = c.get(java.util.Calendar.SECOND);
String sStr = addZero(s);

return ("" + y + "-" + mStr + "-" + dStr + "_" + hStr + "-" + mnStr + "-" + sStr);
}

private static String addZero(int d) {
String dStr;
if (d < 10) {
dStr = "0" + d;
} else {
dStr = "" + d;
}
return dStr;
}
}




Source: http://www.robohobby.com/java_me_calendar_to_string_util.jsp
Category: Java | Added by: penta (2010-11-01)
Views: 457 | Tags: string ke calendar, calendar ke string | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *:
Search
Site friends
  • Create a free website
  • Copyright Penta.Inc © Powered By: Senjapala