android format date code example

Example 1: android java format date time

import java.time.*;
import java.time.format.*;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime datetime = LocalDateTime.now();
String output = formatter.format(datetime);

Example 2: android java parse date time

import java.time.*;
import java.time.format.*;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String input = "13:07:35";
LocalTime time = LocalTime.parse(input, formatter);

Example 3: android java parse date time

import java.time.*;
import java.time.format.*;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String input = "2021-02-05";
LocalDate date = LocalDate.parse(input, formatter);

Example 4: android java format date time

import java.time.*;
import java.time.format.*;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.now();
String output = formatter.format(date);

Example 5: android java format date time

import java.time.*;
import java.time.format.*;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime time = LocalTime.now();
String output = formatter.format(time);

Example 6: date format in android

startTime = "2013-02-27 21:06:30";
StringTokenizer tk = new StringTokenizer(startTime);
String date = tk.nextToken();  
String time = tk.nextToken();

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
Date dt;
try {    
    dt = sdf.parse(time);
    System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here
} catch (ParseException e) {
    e.printStackTrace();
}

Tags:

Misc Example