---
title: "Datumkoll"
type: "forum-thread"
url: "https://www.webforum.nu/amne/java/164001-datumkoll"
topic: "Java"
topic_url: "https://www.webforum.nu/amne/java"
author: "crazyl"
published: "2007-08-29T14:17:11.000Z"
updated: "2007-09-05T18:46:42.000Z"
replies: 4
views: 711
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/java/164001-datumkoll"
---

# Datumkoll

## #1 — crazyl, 2007-08-29T14:17Z

Hej,

Jag har en sträng i formatet tex "19710210" och jag vill kolla om den motsvarar ett datum. Hur gör jag? 

Mvh

Permalänk: https://www.webforum.nu/p/164001

## #2 — dAEk, 2007-08-30T14:05Z

Kolla på klassen SimpleDateFormat och använd "yyyyMMdd" (pattern) för att parsa din datumsträng. Det borde väl funka?

Permalänk: https://www.webforum.nu/p/2020902

## #3 — dAEk, 2007-09-01T18:45Z

Jag var bara en bit på vägen, tydligen. Det kan t.ex. vara av intresse att veta om det är ett giltigt datum också.  ;)

I alla fall. Här har du [ett exempel från Suns forum](http://forum.java.sun.com/thread.jspa?forumID=45&threadID=241847): 

```
	public static boolean isDateValid(String date, String format) {
		  try {
			  Date dateSimple = new SimpleDateFormat(format).parse(date);
			  
			  Format formatter = new SimpleDateFormat(format);
			  
			  if (!date.equals(formatter.format(dateSimple)))
				  return false;
 
			  return true;
		  }
		  catch(ParseException e) {
		    return false;
		  }
		}
	
	public static void main(String[] args) {
		if (!Utilities.isDateValid("30042006","ddMMyyyy"))
		{
			System.out.println("30-04 is not valid.");
		}
		if (!Utilities.isDateValid("31042006","ddMMyyyy"))
		{
			System.out.println("31-04 is not valid.");
		}		
	}
```

 Det behövs verkligen ett vettigt API för datumhantering i Java.

Permalänk: https://www.webforum.nu/p/2021461

## #4 — TomasJ, 2007-09-04T22:48Z

> **dAEk skrev:**
>
> ...
> 
> Det behövs verkligen ett vettigt API för datumhantering i Java.

<http://joda-time.sourceforge.net/>
\>Joda-Time provides a quality replacement for the Java date and time classes.
\>...
\>JSR 310 has now been launched. It aims to build upon Joda-Time and include it in the JDK.

Så här kan man använda Joda Time för att kontrollera om en datumsträng motsvarar ett datum:

```
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateTimeUtility {

	/**
	 * @param dateString a date, e.g. "19710210" (if the dateFormat String would be "yyyyMMdd"). 
	 * If there is whitespace in the beginning or end it will be ignored, e.g. " 19710210 " should be parsed as a valid DateTime.
	 * @param dateFormat a date format e.g. "yyyyMMdd"
	 * @return a valid DateTime object if the dateString represents a correct date according to the dateFormat parameter.
	 * @throws  RuntimeException will be thrown if the input parameters will not be able to return a valid DateTime.
	 */
	public static DateTime getDateTime(final String dateStringInput, final String dateFormat) 
		throws RuntimeException { // e.g. IllegalFieldValueException
		
		final String dateString = (dateStringInput == null) ? "" : dateStringInput.trim();
		
		final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dateFormat);
		final DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
		return dateTime;
	}

	public static boolean isDateValid(final String dateString, final String dateFormat) {
		try {
			// final DateTime dateTime = getDateTime(dateString, dateFormat);
			// return dateTime != null;
			getDateTime(dateString, dateFormat); // return value is not needed here
			return true; 
		} catch (Exception e) { // e.g. IllegalFieldValueException
			return false;
		}
	}
}

//JUnit 4
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class DateTimeUtilityTest {
	
	private final static String DATEFORMAT = "yyyyMMdd";
	
	@Test
	public final void testIsDateValid() {
		assertTrueDates("19710228", "20070228", "20070430", "20080228", "20080229", "20080229 ", " 20080229", " 20080229 "); // 2008 är skottår
		assertFalseDates("19710229", "20070229", "20070431", "20080230", null, "", "a", "19710228a", "a19710228");
	}

	private void assertTrueDates(String... dateStrings) {
		for(String dateString: dateStrings) {
			assertTrue(dateString, DateTimeUtility.isDateValid(dateString, DATEFORMAT));
		}
	}
	
	private void assertFalseDates(String... dateStrings) {
		for(String dateString: dateStrings) {
			assertFalse(dateString, DateTimeUtility.isDateValid(dateString, DATEFORMAT));
		}
	}	
}
```

/ Tomas

Permalänk: https://www.webforum.nu/p/2022606

## #5 — Lime, 2007-09-05T18:46Z

Jag tänker verkligen rösta nej till Joda-Time... ;)

Permalänk: https://www.webforum.nu/p/2022827

---

Tråden på webben: https://www.webforum.nu/amne/java/164001-datumkoll
