BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Date – Convert 12 hour format to 24 hour format and vice versa

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will see how to convert 12 hour format to 24 hour format and 24 hour format to 12 hour format in Java.

Java – Convert 12 Hour data time format to 24 hour date time format

We can change the pattern in the SimpleDateFormat for the conversion. The pattern dd/MM/yyyy hh:mm:ss aa is used for the 12 hour format and the pattern MM-dd-yyyy HH:mm:ss is used for the 24 hour format. In this program we are changing the format of the date by changing the patterns and formatting the input date.

Input/Output of the Program:
Input date and time: 23/12/2014 12:22:12 PM (12 hour format)
Output date and time: 12-23-2014 22:22:12 (24 hour format)

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.text.ParseException;
class DateTimeFormatDemo
{
   public static void main(String[] args)
   {
      String input = "23/12/2014 10:22:12 PM";
      //Format of the date defined in the input String
      DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
      //Desired format: 24 hour format: Change the pattern as per the need
      DateFormat outputformat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
      Date date = null;
      String output = null;
      try{
         //Converting the input String to Date
    	 date= df.parse(input);
         //Changing the format of date and storing it in String
    	 output = outputformat.format(date);
         //Displaying the date
    	 System.out.println(output);
      }catch(ParseException pe){
         pe.printStackTrace();
       }
   }
}

Output:

12-23-2014 22:22:12

Java – Convert 24 Hour format to 12 hour format

In this example we are doing the opposite of what we have done in the above example. Here we are converting the 24 hour format of input date into a 12 hour format.

Input/Output of the program:
Input date and time: 15/02/2014 22:22:12(24 hour format dd/MM/yyyy HH:mm:ss)
Output date and time: 2014-02-15 10:22:12 PM (12 hour format yyyy-MM-dd hh:mm:ss aa)

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.text.ParseException;
class Example2
{
   public static void main(String[] args)
   {
       //Input date in String format
       String input = "15/02/2014 22:22:12";
       //Date/time pattern of input date
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       //Date/time pattern of desired output date
       DateFormat outputformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
       Date date = null;
       String output = null;
       try{
          //Conversion of input String to date
    	  date= df.parse(input);
          //old date format to new date format
    	  output = outputformat.format(date);
    	  System.out.println(output);
    	}catch(ParseException pe){
    	    pe.printStackTrace();
    	 }
   }
}

Output:

2014-02-15 10:22:12 PM

Related Posts:

  1. Java – SimpleDateFormat examples
  2. Java – Convert String to 24 hour date format
  3. Java – Convert Date to String
  4. Java – Convert String to Date
  5. Java 8 – Calculate Days between two dates

Top Related Articles:

  1. Date validation in java
  2. Java Access Modifiers – Public, Private, Protected & Default
  3. Java 8 – Calculate days between two dates
  4. Compare two dates with each other in Java
  5. Java Date Validation Example

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Kushagra says

    November 12, 2015 at 7:07 PM

    very thanks the most simple method i found. Can you tell why we have to use try-catch block.

    Reply
  2. Eric says

    May 16, 2017 at 8:43 PM

    I accidentally landed in this page and found the comment. Thought I would give my reply since no one replied to the comment!

    Answer for reason for try-catch block:
    Since the input in the program is hardcoded, technically we wouldn’t need a try catch block here, however no program in any world would hardcode a value like this. And hence, the developer has thought in advance that the input will definitely be parameterized. That means, there is a margin for error which is why we have the try-catch block!! Hope I’m clear!!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap