All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Set Date Format dd-mm-yyyy In Android

Last Updated : Jul 17, 2023

How To Set Date Format dd-mm-yyyy In Android

In this article we will show you the solution of how to set date format dd-mm-yyyy in android, the SimpleDateFormat Java library is used in Android to format date and time, and a Calendar object is used to aid obtain the most recent system time and date.

The present time and the date belong to the type Long, which can be transformed into a date and time that can be read by humans.

A class called SimpleDateFormat is available in the java.text package and is used to format & parse dates in the appropriate way (local).

You can format Date into String or parse a String to a Date through the methods of this class.

By utilizing its parse() method of a SimpleDateFormat class, you can convert a given String to a Date object.

You must give the Date on String format to this String object to the a Date object conversion.

Step By Step Guide On How To Set Date Format dd-mm-yyyy In Android :-

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Scanner;
public class CalculatingAge {
   public static Date StringToDate(String dob) throws ParseException{
      SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      Date date = formatter.parse(dob);
      System.out.println("Date object value: "+date);
      return date;
   public static void main(String args[]) throws ParseException {
      Scanner sc = new Scanner(;
      System.out.println("Enter your name: ");
      String name =;
      System.out.println("Enter your date of birth (dd-MM-yyyy): ");
      String dob =;
      Date date = CalculatingAge.StringToDate(dob);
      Instant instant = date.toInstant();
      ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());
      LocalDate givenDate = zone.toLocalDate();
      Period period = Period.between(givenDate,;
      System.out.print("Hello "+name+" your current age is: ");
      System.out.print(period.getYears()+" years "+period.getMonths()+" and "+period.getDays()+" days");
  1. First you create an import function.
  2. Then we create a class and Instantiating the SimpleDateFormat class.
  3. Then we Parsing the given String to Date object.
  4. Then we read the name and date of birth from the user.
  5. After that we Converted String to Date.
  6. Then we convert the obtained Date object to LocalDate object.
  7. Then we calculate the difference between the given date and the current date.
  8. After that we close the program using the system.out.print function.

Conclusion :-

The Java SimpleDateFormat class provides methods for formatting and parsing dates and times.

SimpleDateFormat is indeed a concrete class which derives from Java.text and can be utilized to format or parse dates. DateFormat class.

It should be noted that formatting entails turning a date to a string, whereas parsing entails converting a string to a date. SimpleDateFormat(String pattern args): Creates an instance of a SimpleDateFormat class that uses the pattern pattern args, which are the standard date format symbols for the FORMAT locale.

I hope this article on how to set date format dd-mm-yyyy in android helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪