// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: DateFormatTest.jlc,v 1.7 2003/10/12 13:16:39 heiko Exp $ */ #include "Test.h" #include #include #include using namespace jakelib::lang; using namespace jakelib::util; using namespace jakelib::text; #pragma javasyntax void testSimpleDateFormat(Locale *loc, Calendar *cal, String *pattern = null) { SimpleDateFormat *sdf; if (loc != null) Locale::setDefault(loc); if (pattern != null) sdf = new SimpleDateFormat(pattern); else { DateFormatSymbols *syms = new DateFormatSymbols(Locale::getDefault()); sdf = new SimpleDateFormat(syms->getDateTimeFormats()->get(DateFormat::FULL)); } System::out->println("" .. loc .. ": " .. sdf->format(cal->getTime())); } void dateFormatTest() { try { String *germanShort = "dd.MM.yyyy HH:mm"; String *germanMedium = "dd.MM.yyyy HH:mm:ss.SSS"; String *germanLong = "dd. MMMM yyyy HH:mm:ss.SSS"; GregorianCalendar *cal1 = new GregorianCalendar(2003, Calendar::SEPTEMBER, 20, 9, 52); testSimpleDateFormat(null, cal1, germanMedium); GregorianCalendar *cal2 = new GregorianCalendar(2003, Calendar::JANUARY, 3, 15, 5); testSimpleDateFormat(null, cal2, germanShort); testSimpleDateFormat(null, new GregorianCalendar(), germanMedium); testSimpleDateFormat(null, new GregorianCalendar(), germanLong); testSimpleDateFormat(null, new GregorianCalendar()); // Use current date/time: SimpleDateFormat *sdf = new SimpleDateFormat(germanMedium); GregorianCalendar *cal0 = new GregorianCalendar(); JTEST_RETURN_STRING("Testing SimpleDateFormat \"" .. germanMedium .. "\"", sdf->format(cal0->getTime()), String::printf("%02i.%02i.%04i %02i:%02i:%02i.%03i", cal0->get(Calendar::DATE), cal0->get(Calendar::MONTH) +1, cal0->get(Calendar::YEAR), cal0->get(Calendar::HOUR_OF_DAY), cal0->get(Calendar::MINUTE), cal0->get(Calendar::SECOND), cal0->get(Calendar::MILLISECOND))); System::out->println("Testing some Locales:"); testSimpleDateFormat(Locale::GERMANY, cal0); testSimpleDateFormat(Locale::FRANCE, cal0); testSimpleDateFormat(Locale::US, cal0); testSimpleDateFormat(Locale::UK, cal0); testSimpleDateFormat(new Locale("es"), cal0); System::out->println("new Date(): " .. cal0->getTime()); System::out->println("new Date(): " .. new Date()); System::out->println("Doing som SimpleDateFormat parsing:"); // first: System::out->println("Parsing \"17.Mai.1978 12:30:40\" with \"dd.MMMM.yyyy HH:mm:ss\": "); Date * hdate = (new SimpleDateFormat("dd.MMMM.yyyy HH:mm:ss", Locale::GERMANY))->parse("17.Mai.1978 12:30:40", null); Calendar * hCal = new GregorianCalendar(); hCal->setTime(hdate); testSimpleDateFormat(Locale::GERMANY, hCal); // second: System::out->println("Parsing \"25.01.76 05:30:40 PM\" with \"dd.MM.yy hh:mm:ss aa\": "); hdate = (new SimpleDateFormat("dd.MM.yy hh:mm:ss aa", Locale::GERMANY))->parse("25.01.76 05:30:40 PM", null); hCal->setTime(hdate); testSimpleDateFormat(Locale::GERMANY, hCal); // third: System::out->println("Parsing \"25.01.76\" with \"dd.MM.yy\": "); hdate = (new SimpleDateFormat("dd.MM.yy", Locale::GERMANY))->parse("25.01.76", null); hCal->setTime(hdate); testSimpleDateFormat(Locale::GERMANY, hCal); } catch (Exception *ex) { System::out->println(ex); } }