Java tutorial
//package com.java2s; /* * Copyright (C) 2013 km innozol IT solutions Pvt Ltd <http://innozol.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Calendar; public class Main { /** * Date from database as string can convert to calendar * @param date A formated (yyyy-MM-dd HH:mm:ss) date from database * @return A converted calendar object */ public static Calendar stringToCalendar(String date) { Calendar calendar = Calendar.getInstance(); String[] dateTime = date.split(" "); String[] s_date = dateTime[0].split("-"); String[] s_time = dateTime[1].split(":"); calendar.set(toInt(s_date[0]), (toInt(s_date[1]) - 1), toInt(s_date[2]), toInt(s_time[0]), toInt(s_time[1]), toInt(s_time[2])); return calendar; } private static int toInt(String string) { return Integer.parseInt(string); } }