How to extract Day, Month and Year values from a string [like 18/08/2012]. I tried using SimpleDateFormat, but it returns a Date object and I observed that all the Get methods are deprecated. Is there any better way to do this?
Thanks
Personally I'd use Joda Time, which makes life considerably simpler. In particular, it means you don't need to worry about the time zone of the Calendar
vs the time zone of a SimpleDateFormat
- you can just parse to a LocalDate
, which is what the data really shows you. It also means you don't need to worry about months being 0-based :)
Joda Time makes many date/time operations much more pleasant.
import java.util.*;
import org.joda.time.*;
import org.joda.time.format.*;
public class Test {
public static void main(String[] args) throws Exception {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy")
.withLocale(Locale.UK);
LocalDate date = formatter.parseLocalDate("18/08/2012");
System.out.println(date.getYear()); // 2012
System.out.println(date.getMonthOfYear()); // 8
System.out.println(date.getDayOfMonth()); // 18
}
}
Simply go for String.split()
,
String str[] = "18/08/2012".split("/");
int day = Integer.parseInt(str[0]);
int month = Integer.parseInt(str[1]);
..... and so on
This should get you going without adding external jars
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date parse = sdf.parse("18/08/2012");
Calendar c = Calendar.getInstance();
c.setTime(parse);
System.out.println(c.get(Calendar.MONTH) + c.get(Calendar.DATE) + c.get(Calendar.YEAR));
java.time.LocalDate.parse(
"18/08/2012" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
).getDayOfMonth() // .getYear() .getMonth()
The modern approach uses the java.time classes. Avoid the troublesome legacy classes such as Date
& Calendar
.
LocalDate
String input = "18/08/2012" ;
The LocalDate
class represents a date-only value without time-of-day and without time zone.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
ld.toString(): 2012-08-18
Interrogate for the parts.
int d = ld.getDayOfMonth() ;
int m = ld.getMonthValue() ;
int y = ld.getYear() ;
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Create a java.util.Calendar object out of that date as follows and extract the date parts:
Calendar cal = Calendar.getInstance();
cal.setTime(<date from simple-date-format).
cal.get(Calendar.MONTH);
etc.,
Another approach may be use Calendar object get(Calendar.MONT)
Example:
Calendar cal = Calendar.getInstance();
cal.setTime(dateObj).
cal.get(Calendar.MONTH);
(or)
You may use String.split() also.
Use This And Pass the date Value
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());
Date parse = sdf.parse("18/01/2018");
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
int date = calendar.get(Calendar.DATE);
//+1 Is Important Because if the month is January then coming 0 so Add +1
int month = calendar.get(Calendar.MONTH)+1;
int year = calendar.get(Calendar.YEAR);
System.out.println("Date:"+date +":Month:"+ month + ":Year:"+year);
In it the String is stored in an array in form of elements, and with the help of split() function, I have separated it and retrieved it from the array str[] and stored in 3 different variables day, month & year.
import java.util.*;
public class date {
public static void main(String[] args) throws Exception {
String str[] = "18/08/2012".split("/");
int day = Integer.parseInt(str[0]);
int month = Integer.parseInt(str[1]);
int year = Integer.parseInt(str[2]);
System.out.println(day);
System.out.println(month);
System.out.println(year);
}
}