In this Java tutorial, nosotros volition larn how to parse String to Date using Joda-Time library, for example, nosotros volition convert appointment String "04-12-2014" to java.util.Date object which represents this date. Before Java 8 introduced its new Date as well as Time API, Joda was exclusively reliable, prophylactic as well as slowly means to bargain alongside appointment as well as fourth dimension intricacies inward Java. Java's ain Date as well as Time was non that great, starting from JDK 1.1 when they made java.util.Date a mutable object as well as when they introduced Calendar inward Java 1.2. It is 1 of the most criticized characteristic of Java on communities along alongside checked exception as well as object cloning. Even though Java 8 has corrected its error alongside an excellent, shiny novel API, which addresses all past times issue, Joda appointment as well as fourth dimension library nonetheless has a purpose to play inward Java systems. First as well as firstly argue is because most of the large banks as well as clients are nonetheless running on Java 1.6 as well as volition probable convey some other five to half-dozen twelvemonth to adopt Java 8, Joda is the exclusively friend yous tin trust to bargain alongside appointment as well as fourth dimension nightmares.
One of the most mutual chore inward Java is to parse String to Dates as well as fifty-fifty though Java provides a utility class called SimpleDateFormat, its non prophylactic to occupation inward multi-threaded surroundings until yous know how to occupation thread confinement to brand SimpleDateFormat thread-safe.
Many beginners either practise novel event of SimpleDateFormat each fourth dimension they conduct hold to convert String to Date or commit classical error of storing it inward an event or static variable, exclusively to confront mysterious issues later. Since most of the Joda Time classes are Immutable, yous tin occupation them easily as well as safely inward concurrent application.
Btw, if yous are running on Java SE 8 hence yous should occupation novel Date as well as Time API equally it's going to endure the criterion inward coming years as well as it volition also take the dependency on a 3rd political party library. You tin also convey assist of Java SE 8 for Really Impatient past times Cay S. Horstmann to larn to a greater extent than virtually this novel library, it's quite similar to Joda time.
If yous are non using Maven hence only add together joda-time-2.5.jar inward your Java program's classpath past times using either CLASSPATH surroundings variable or -classpath selection of coffee command.
Its really of import to maintain the JAR file at both compile fourth dimension as well as runtime to avoid nasty ClassNotFoundException as well as NoClassDefFoundError. Once yous done that, your IDE volition guide yous spell using Joda-Time library. You tin encounter on the topographic point Java documentation to determine which method to occupation as well as why.
As I said earlier, if yous are running on JDK 8, yous should prefer novel Date as well as Time API of Java 8 instead of using Joda appointment as well as fourth dimension API. It's improve to stick alongside criterion JDK classes as well as API if possible. To larn to a greater extent than virtually this fantabulous appointment as well as fourth dimension API, I advise reading Java SE 8 for Really Impatient past times Cay S. Horstmann, 1 of the improve books to larn novel features of Java 8.
You tin encounter its really unproblematic as well as prophylactic to parse String to Date using Joda-time library. If yous desire to modify the appointment format e.g. from "dd/MM/yyyy" to "yyyyMMdd" hence yous demand to telephone phone the forPattern() 1 time again as well as acquire a novel DateTimeFormatter instance.
You tin encounter that nosotros are successfully able to convert "04/12/2014" into a java.util.Date event of same value, as well as nosotros are fifty-fifty to modify the appointment format to convey String equally "yyy/MM/dd" as well as successfully parse it too.
That's all about how to parse String to Date inward Java using Joda Date as well as Time library. If yous know this library, it would endure pretty slowly to larn the novel Java appointment as well as fourth dimension library introduced inward Java 8. It is truly inspired past times JodaTime as well as continued the tradition of immutable, thread-safe as well as less confusing appointment fourth dimension API.
Further Learning
Complete Java Masterclass
solution)How to convert java.sql.Date to java.util.Date inward Java? (solution) How to convert java.util.Date to java.sql.Date inward Java? (solution) How to display appointment inward multiple Timzone inward Java? (example) How to acquire electrical flow month, year, twenty-four hr catamenia as well as twenty-four hr catamenia of calendar week inward Java? (solution) Difference betwixt java.sql.Time, java.sql.Timestamp as well as java.util.Date inward Java? (answer) How to compare 2 dates inward Java? (solution) SimpleDateFormat is non thread-safe, Use it carefully? (answer) How to convert local fourth dimension to GMT fourth dimension inward Java? (solution) How to add, subtract days, calendar month as well as twelvemonth from appointment inward Java? (example) How to convert XMLGregorianCalendar to Date inward Java? (solution) How to convert millisecond to Date inward Java? (solution)
One of the most mutual chore inward Java is to parse String to Dates as well as fifty-fifty though Java provides a utility class called SimpleDateFormat, its non prophylactic to occupation inward multi-threaded surroundings until yous know how to occupation thread confinement to brand SimpleDateFormat thread-safe.
Many beginners either practise novel event of SimpleDateFormat each fourth dimension they conduct hold to convert String to Date or commit classical error of storing it inward an event or static variable, exclusively to confront mysterious issues later. Since most of the Joda Time classes are Immutable, yous tin occupation them easily as well as safely inward concurrent application.
Btw, if yous are running on Java SE 8 hence yous should occupation novel Date as well as Time API equally it's going to endure the criterion inward coming years as well as it volition also take the dependency on a 3rd political party library. You tin also convey assist of Java SE 8 for Really Impatient past times Cay S. Horstmann to larn to a greater extent than virtually this novel library, it's quite similar to Joda time.
How to occupation Joda-Time inward Java - JAR as well as dependency
In social club to occupation Joda-Time library inward your Java program, yous demand to root download the JAR file from http://www.joda.org/joda-time/, or alternatively if yous are using Maven hence yous tin also add together next dependency inward your pom.xml as well as it volition convey aid of downloading as well as managing dependencies for you.<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.5</version> </dependency>
If yous are non using Maven hence only add together joda-time-2.5.jar inward your Java program's classpath past times using either CLASSPATH surroundings variable or -classpath selection of coffee command.
Its really of import to maintain the JAR file at both compile fourth dimension as well as runtime to avoid nasty ClassNotFoundException as well as NoClassDefFoundError. Once yous done that, your IDE volition guide yous spell using Joda-Time library. You tin encounter on the topographic point Java documentation to determine which method to occupation as well as why.
As I said earlier, if yous are running on JDK 8, yous should prefer novel Date as well as Time API of Java 8 instead of using Joda appointment as well as fourth dimension API. It's improve to stick alongside criterion JDK classes as well as API if possible. To larn to a greater extent than virtually this fantabulous appointment as well as fourth dimension API, I advise reading Java SE 8 for Really Impatient past times Cay S. Horstmann, 1 of the improve books to larn novel features of Java 8.
Parsing String to Date inward Java using Joda Time
Here is my sample Java program, which converts String dates into util appointment inward Java. It's footling flake similar to my before program, how to convert String to Date inward Multithreaded Java program, which was using SimpleDateFormat for parsing, but hither nosotros are using Joda-Time DateTimeFormat as well as DateTimeFormatter classes. Here are the steps to parse String to Date using Joda library :- Create a appointment blueprint using forPattern() method of DateTimeFormat class.
- forPattern() method returns an object of DateTimeFormatter which does actual parsing.
- Use parseMillis() method of DateTimeFormatter class to convert String to long millisecond
- Now nosotros tin practise a novel java.util.Date() object using this millisecond value
You tin encounter its really unproblematic as well as prophylactic to parse String to Date using Joda-time library. If yous desire to modify the appointment format e.g. from "dd/MM/yyyy" to "yyyyMMdd" hence yous demand to telephone phone the forPattern() 1 time again as well as acquire a novel DateTimeFormatter instance.
import java.text.ParseException; import java.util.Date; import java.util.Scanner; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; /** * How to parse String to appointment inward Java using JodaTime library * * @author WINDOWS 8 */ public class JodaDateParser{ public static void main(String args[]) throws ParseException{ String blueprint = "dd/MM/yyyy"; System.out.printf("Please come inward appointment inward %s format %n", pattern); Scanner reader = new Scanner(System.in); String aDate = reader.nextLine(); DateTimeFormatter df =DateTimeFormat.forPattern("dd/MM/yyyy"); long millis = df.parseMillis(aDate); Date appointment = new Date(millis); System.out.printf("String %s is equal to appointment %s inward Java %n", aDate, date); // let's practise this for some other appointment format e.g. yyyy/MM/dd blueprint = "yyyy/MM/dd"; System.out.printf("Please come inward appointment inward %s format %n", pattern); aDate = reader.nextLine(); // needs to practise some other DateTimeFormatter alongside novel pattern df =DateTimeFormat.forPattern("yyyy/MM/dd"); millis = df.parseMillis(aDate); appointment = new Date(millis); System.out.printf("String %s is equal to appointment %s inward Java %n", aDate, date); reader.close(); } } Output Please come inward appointment inward dd/MM/yyyy format 04/12/2014 String 04/12/2014 is equal to appointment Thu Dec 04 00:00:00 GMT+08:00 2014 inward Java Please come inward appointment inward yyyy/MM/dd format 2014/12/03 String 2014/12/03 is equal to appointment Wed Dec 03 00:00:00 GMT+08:00 2014 inward Java
You tin encounter that nosotros are successfully able to convert "04/12/2014" into a java.util.Date event of same value, as well as nosotros are fifty-fifty to modify the appointment format to convey String equally "yyy/MM/dd" as well as successfully parse it too.
What volition hap if String is an invalid Date?
If yous travail to parse an invalid appointment String using Joda time, yous volition definitely acquire an error something similar ParseException of JDK. In fact Joda Time throws IllegalArgumentException similar this when yous travail to parse an invalid appointment using DateTimeFormatter class : "java.lang.IllegalArgumentException: Invalid format" error inward Java. For illustration inward next snippet, our appointment format is dd/MM/yyyy but nosotros entered appointment inward some other format e.g. yyyy/MM/dd as well as that's why DateTimeFormatter from Joda is non able to parse it.Please come inward date in dd/MM/yyyy format 2014/11/23 Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2014/11/23" is malformed at "14/11/23" at org.joda.time.format.DateTimeParserBucket.doParseMillis(DateTimeParserBucket.java:187) at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:780) at JodaDateParser.main(JodaDateParser.java:26)
Further Learning
Complete Java Masterclass
solution)
No comments:
Post a Comment