Str to date java

How to convert String to Date – Java

In this tutorial, we will show you how to convert a String to java.util.Date . Many Java beginners are stuck in the Date conversion, hope this summary guide will helps you in some ways.

 // String -> Date SimpleDateFormat.parse(String); // Date -> String SimpleDateFormat.format(date); 

Refer to table below for some of the common date and time patterns used in java.text.SimpleDateFormat , refer to this JavaDoc

Letter Description Examples
y Year 2013
M Month in year July, 07, 7
d Day in month 1-31
E Day name in week Friday, Sunday
a Am/pm marker AM, PM
H Hour in day 0-23
h Hour in am/pm 1-12
m Minute in hour 0-60
s Second in minute 0-60

1. String = 7-Jun-2013

If 3 ‘M’, then the month is interpreted as text (Mon-Dec), else number (01-12).

 package com.mkyong.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDateExample1 < public static void main(String[] argv) < SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); String dateInString = "7-Jun-2013"; try < Date date = formatter.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); >catch (ParseException e) < e.printStackTrace(); >> > 
 Fri Jun 07 00:00:00 MYT 2013 07-Jun-2013 

2. String = 07/06/2013

 package com.mkyong.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDateExample2 < public static void main(String[] argv) < SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String dateInString = "07/06/2013"; try < Date date = formatter.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); >catch (ParseException e) < e.printStackTrace(); >> > 
 Fri Jun 07 00:00:00 MYT 2013 07/06/2013 

3. String = Fri, June 7 2013

 package com.mkyong.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDateExample3 < public static void main(String[] argv) < SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy"); String dateInString = "Fri, June 7 2013"; try < Date date = formatter.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); >catch (ParseException e) < e.printStackTrace(); >> > 
 Fri Jun 07 00:00:00 MYT 2013 Fri, Jun 07 2013 

4. String = Friday, Jun 7, 2013 12:10:56 PM

 package com.mkyong.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDateExample4 < public static void main(String[] argv) < SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a"); String dateInString = "Friday, Jun 7, 2013 12:10:56 PM"; try < Date date = formatter.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); >catch (ParseException e) < e.printStackTrace(); >> > 
 Fri Jun 07 12:10:56 MYT 2013 Friday, Jun 07, 2013 12:10:56 PM 

5. String = 2014-10-05T15:23:01Z

Z suffix means UTC, java.util.SimpleDateFormat doesn’t parse it correctly, you need to replace the suffix Z with ‘+0000’.

 package com.mkyong.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDateExample5 < public static void main(String[] argv) < SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); String dateInString = "2014-10-05T15:23:01Z"; try < Date date = formatter.parse(dateInString.replaceAll("Z$", "+0000")); System.out.println(date); System.out.println("time zone : " + TimeZone.getDefault().getID()); System.out.println(formatter.format(date)); >catch (ParseException e) < e.printStackTrace(); >> > 
 Sun Oct 05 23:23:01 MYT 2014 time zone : Asia/Kuala_Lumpur 2014-10-05T23:23:01+0800 

In Java 8, you can convert it into a java.time.Instant object, and display it with a specified time zone.

 package com.mkyong.date; import java.time.*; public class TestDateExample6 < public static void main(String[] argv) < String dateInString = "2014-10-05T15:23:01Z"; Instant instant = Instant.parse(dateInString); System.out.println(instant); //get date time only LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId())); System.out.println(result); //get date time + timezone ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Africa/Tripoli")); System.out.println(zonedDateTime); //get date time + timezone ZonedDateTime zonedDateTime2 = instant.atZone(ZoneId.of("Europe/Athens")); System.out.println(zonedDateTime2); >> 
 2014-10-05T15:23:01Z 2014-10-05T15:23:01 2014-10-05T17:23:01+02:00[Africa/Tripoli] 2014-10-05T18:23:01+03:00[Europe/Athens] 

References

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Читайте также:  Сортировка php mysql таблиц

Comments

How to convert below string “2020-11-22T05:57:10+01:00” using Java8 ?

Hi I have a requirement to convert DateofBirth, StartDate and TerminationDate fields in a file with about 100 lines. The current date format is 00.00.0000 and the requirement is 00/00/0000.

Thanks. This came in handy for me!

Hi, in your program you have DateFormat defined as ‘dd-MMM-yyyy’ then how come 23-September-2015 is a valid date as well? I’ve tried setting up lenient to false but that still won’t work, any suggestions please?

Hi
How to handle the format “2020–03–01 3:15 pm”?

Yoo solor quiero insertar una hora en mi base de datos y no entiendo nada!! el dato de mi base de datos es date, pero solo quiero pner hora por ejemplo 2:30 solo eso

In Method 4 4. String = Friday, Jun 7, 2013 12:10:56 PM
my requirement is if time is less than 12 (Friday, Jun 7, 2013 11:10:56 PM ) i am getting Unparseable date:
But in 12 hours format that is how we will write date.

Hello Sir,
I would like to ask, how to convert six june 1990 to 06/06/1990. Thanks

Do any one know what will be the formatter for this: 2018-06-19 14:59:29.0T-07:00
Thank you!

Hello Sir,
In example 5th the string i have passed is String = 14-10-05T15:23:01Z so in this case the output which i am getting is
Sun Oct 05 23:23:01 MYT 0014
time zone : Asia/Kuala_Lumpur
0014-10-05T23:23:01+0800
instead of parse exception which is causing data corruption. Please help how to handle this case ?

hello in my DB i Have declared month like a Long type ..so in my function I wan to use month like a Long value not String ..example : July i need it 07
How to proceed for that conversion ?

Hi Mkyong, Thanks. This came in handy for me!

Can you please guide me here –

import java.util.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/*Java date format issue
Input- 2016-11-23 23:38:03.850454 having milisec and nano in format
output- 20161123233803850454 in format YYYYMMDDHHmmssSSSSSS
my output- 201612361235213000454 which is incorrect , need help
*/
public class TestingDate
public static void main(String[] args) throws ParseException
TestingDate obj = new TestingDate();
System.out.println(obj.check(“2016-11-23 23:38:03.850454”));
>
public String check(String string) throws ParseException
String dateStr = string;
DateFormat srcDf = new SimpleDateFormat(“YYYY-MM-DD HH:mm:ss.SSSSSS”);
Date date = srcDf.parse(dateStr);
DateFormat destDf = new SimpleDateFormat(“YYYYMMDDHHmmssSSSSSS”);
dateStr = destDf.format(date);
return dateStr ;
>
>

Источник

Str to date java

Let’s see another code to convert different types of Strings to Date in java. Here, we have used different date formats using SimpleDateFormat class.

31/12/1998 Thu Dec 31 00:00:00 IST 1998 31-Dec-1998 Thu Dec 31 00:00:00 IST 1998 12 31, 1998 Thu Dec 31 00:00:00 IST 1998 Thu, Dec 31 1998 Thu Dec 31 00:00:00 IST 1998 Thu, Dec 31 1998 23:37:50 Thu Dec 31 23:37:50 IST 1998 31-Dec-1998 23:37:50 Thu Dec 31 23:37:50 IST 1998

Youtube

For Videos Join Our Youtube Channel: Join Now

Feedback

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью