Reputation: 1619
I want current time in millis and then to store it in 12 hour format but with this piece of code I am getting 24 hour format time.
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss a");
dateforrow = dateFormat.format(cal1.getTime());
can anybody suggest modifications to get the desired results?
Upvotes: 58
Views: 169404
Reputation: 79155
There are three major problems with your code:
m
[Minute in hour] at the place of M
[Month in year].H
[Hour in day (0-23)] instead of h
[Hour in am/pm (1-12)]. Check the documentation to learn more about these two points.Locale
with SimpleDateFormat
. Check Never use SimpleDateFormat or DateTimeFormatter without a Locale to learn more about it.So, the instantiation with the correct format would be:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.ENGLISH);
Note that the java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*, released in March 2014 as part of Java SE 8 standard library.
Solution using java.time
, the modern Date-Time API:
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.now(ZoneOffset.UTC);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/uuuu hh:mm:ss a", Locale.ENGLISH);
String formatted = dtf.format(odt);
System.out.println(formatted);
}
}
Here, you can use y
instead of u
but I prefer u
to y
.
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Upvotes: 2
Reputation: 523
Yep, confirmed that simply using "hh"
instead of "HH"
fixed my issue, Since "hh"
is for 12-Hour Format & "HH"
is for 24-Hour Format.
Changed from this:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm aa");
To this:
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
You can still use "HH"
to store the time if you don't want to bother storing and dealing with the AM/PM. Then when you retrieve it, use "hh"
.
Upvotes: 6
Reputation: 973
Simply follow the code
public static String getFormatedDate(String strDate,StringsourceFormate,
String destinyFormate) {
SimpleDateFormat df;
df = new SimpleDateFormat(sourceFormate);
Date date = null;
try {
date = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat(destinyFormate);
return df.format(date);
}
and pass the value into the function like that,
getFormatedDate("21:30:00", "HH:mm", "hh:mm aa");
or checkout this documentation SimpleDateFormat for StringsourceFormate and destinyFormate.
Upvotes: 0
Reputation: 1
See code example below:
SimpleDateFormat df = new SimpleDateFormat("hh:mm");
String formattedDate = df.format(new Date());
out.println(formattedDate);
Upvotes: -1
Reputation: 28823
Change HH
to hh
as
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy hh:mm:ss a");
dateforrow = dateFormat.format(cal1.getTime());
Note that dd/mm/yyyy
- will give you minutes instead of the month.
Upvotes: 155
Reputation: 943
You can try it like this
Calendar c= Calendar.getInstance();
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String str=sdf.format(c.getTime());
Upvotes: 1
Reputation: 25979
I re-encounter this in the hard way as well. H vs h, for 24-hour vs 12 hour !
Upvotes: 7
Reputation: 1971
Hi I tested below code that worked fine :
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss a");
dateFormat.format(cal1.getTime());
Upvotes: 3
Reputation: 117607
Referring to SimpleDataFormat JavaDoc:
Letter | Date or Time Component | Presentation | Examples
---------------------------------------------------------
H | Hour in day (0-23) | Number | 0
h | Hour in am/pm (1-12) | Number | 12
Upvotes: 55
Reputation: 21191
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss a");
use hh
in place of HH
Upvotes: 0