Tips To Do JAVA STRING TO DATE Conversion Like A PRO

Опубликовано: 08 Октябрь 2024
на канале: Begin Secure
128
3

Learn how to do Java String to Date conversion. What to watch out for in the old Java libraries. They can cause really hard to find bugs in your code, not to mention cause security issues in your code. Tune in to find out!!

Come back often for more videos answering common questions around development and application security.

#IT #Development #SoftwareDevelopment #JavaProgramming #JavaTutorial #AppplicationSecurity #AppSec #appsecurity #informationsecurity #infosec

Chapters

00:00 Introduction
00:39 Overview
01:33 Understand Date/Time Format
05:38 Code the solution
12:46 Discuss security aspects

So the question we address in this video is: how can we do a String to date conversion in java?

Let’s say that we have a String like the following:

We assign the string variable myDate equal to the String January first, 2020 with the month January written out in text format.

And let’s say we want to parse that string to read the month, the day and the year. How can we do that?

In contemporary Java, we can make use of the class java.time.format.DateTimeFormatter

In order to read the string January 1, 2020, we would need to create a pattern that represents the
data that we will be parsing

In our example of January 1st, 2020, we can represent that pattern like we see here with 4 M’s

Followed by a d, followed by a comma and 4y’s

Let’s look at each of those symbols in our pattern to see what they mean and how the data will be interpreted. First, is the M for month.

4 M’s means the month of the year

The number of M’s determines how the pattern is expected to look.

Less than 4 characters (less than 4 M’s) will mean our formatter class will expect the shortened form of the month name, for example, “Jan” for “January”

Exactly 4 M characters will mean that we expect the full form of the data and the month name spelled out completely

Exactly 5 M characters will mean that we expect the narrow form of the month. We will see what that means when we get into the coding section of this video

Continuing with our breakdown of the pattern, next, let’s look at the d.

The symbol d indicates that the string should contain the day of the month.

It is a lower case d

A single lower case d is fine for eithter 1 or 2 digit days in a string, so either January one or January 25 for example will both work eaqually well when only one d is specified.

If the string uses 2 dees, it will work for double digit days of the month, like 25, but will fail with a parse exception for single digit days, like 1.

If an upper case D is used, it represent the day of the year, so a number between 1 and 365 or 366 for leap year.

Next in our list of symbols used to parse our original string, is the lower case y.

The lower case y symbol represents the year of the era

The number of y’s should match the number of digits in the year.

For example 4 y’s will represent a 4 digit year and 2 y’s will represent a 2 digit year

An upper case Y is used to parse for a week based year,
ensuring the year has has whole weeks that can span into the next year.

This is a bit of an unusual circumstance that might be used for fiscal years for example in an accounting or government application.