Displaying 24 hour format time? [duplicate] - java

This question already has answers here:
converting date time to 24 hour format
(12 answers)
Closed 8 years ago.
In an Android app I have an internal logging method and I add a time-date stamp to the start of each message . . .
public void logEvent(String sMsg) {
String delegate = "MM/dd/yy hh:mm:ss";
java.util.Date noteTS = Calendar.getInstance().getTime();
String sTod = " " + DateFormat.format(delegate,noteTS);
sMsg = sTod + " " + sMsg;
logEvents.add(sMsg);
. . .
This produces a time-date stamp that looks like "07/25/14 02:58:18". But I want the time to be in 24 hour format, i.e., "14:58:18".
In some systems that's accomplished by using "HH" instead of "hh" so I tried that, i.e.,
String delegate = "MM/dd/yy HH:mm:ss";
... but that just gave me "07/25/14 HH:58:18"
So what do I have to do to get the hours to be in 24-hour format?

Use SimpleDateFormat instead of DateFormat. Refer SimpleDateFormat Javadoc API: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
H Hour in day (0-23) Number 0
"MM/dd/yy HH:mm:ss" should work with SimpleDateFormat.
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
java.util.Date noteTS = Calendar.getInstance().getTime();
String sTod = formatter.format(noteTS);

Try this:
private final SimpleDateFormat sdfTime = new SimpleDateFormat("kk:mm");
...
TEXTVIEW.setTitle(sdfTime.format(new Date()));
http://developer.android.com/reference/java/text/SimpleDateFormat.html

You can use very simple class for it.
Try this:
Time time=new Time();
time.setToNow();
textview.setText(time.hour+":"+time.minute);
Hope be useful for you.:)

Related

How to decode the date in Android? - DATETIMESTAMP [duplicate]

This question already has answers here:
Android: Compare time in this format `yyyy-mm-dd hh:mm:ss` to the current moment
(5 answers)
Conversion of a date to epoch Java [duplicate]
(4 answers)
How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?
(16 answers)
Closed 2 years ago.
The following code gave me Datetimestamp as [ 2020-07-183 17:07:55.551 ]. The issue is with "Day" in Datetimestamp, which has three digits. How to format currentTimeMillis into the right format for day of month?
public String Datetimesetter(long currentTimeMillis, SimpleDateFormat dateFormat) {
dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentTimeMillis);
return dateFormat.format(calendar.getTime());
}
SOLUTION WHICH WORKED FOR ME:
Please visit this link.
This is for the case you are supporting Apps from API level 26 (native support of java.time) or you are willing / allowed to use a backport library of the same functionality.
Then you can use a correct / matching pattern (one that considers three-digit days) like this:
public static void main(String[] args) {
// mock / receive the datetime string
String timestamp = "2020-07-183 17:07:55.551";
// create a formatter using a suitable pattern (NOTE the 3 Ds)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-DDD HH:mm:ss.SSS");
// parse the String to a LocalDateTime using the formatter defined before
LocalDateTime ldt = LocalDateTime.parse(timestamp, dtf);
// and print its default String representation
System.out.println(ldt);
}
which outputs
2020-07-01T17:07:55.551
So I guess the day of year no. 183 was actually July 1st.
your date format is incorrect
dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
change to this
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS.SSS");

JSON date to formatted JAVA date [duplicate]

This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I am trying to get dd.MM.yyyy and hh:mm from 1436536800 but only the time is correct, the date is completely wrong. I don't really understand how this is supposed to work
int dt = time.getInt("start")*1000;
Date date = new Date(dt);
startDate = dateFormat.format(date);
If time.getInt("start") is a valid unix timestamp, you must add "000" to the number. Example: 1436536800 * 1000 = 1436536800000. Then you can use the timestamp to get a Date:
final Date date = new Date(Long.parseLong("1436536800000"));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(sdf.format(date));
Console exit: 10.07.2015 09:00
Assuming the time is correct, it's likely the fact that you're multiplying by 1,000. When creating the date the way you are, it takes in milliseconds. Is it possible that your input is already in milliseconds? (Your current method will be ~2 minutes off if so)
Date date=new Date(1436536800);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
Date you are getting is a JSON string value. follow steps below to format it correctly.
First download Moment.js file and add it in your project.
var date1 = "1436536800"; // your long value contain in this variable.
var date2 = moment(date1).format(MMMM Do YYYY);//It will give you formatted date value.
see more formats below

Java - store current date WITHOUT TIME into a Text file [duplicate]

This question already has answers here:
How to convert current date into string in java?
(9 answers)
Closed 6 years ago.
I really need your help. I have searched and tried every example I could use, but none have worked.
I need to store current date in YYYY-MM-DD format in a text file..the String date has to be a string..
String dateF = "YYYY-MM-DD";
Date dateOnly = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateF);
String date = dateFormat.format(dateOnly);
when I tried that code above.. this the output I got
please|work|2016-04-110|11
please help me...this is my assignment due this Friday ): I just need this date and 2 other things to be done..
thanks :)
your issue comes from the case you used for Y and D. according to the API SimpleDateFormat documentation, you should use d (day in month) instead of D (day in year), in your format definition.
String dateF = "yyyy-MM-dd";
Format being used is incorrect.
YYYY-MM-DD : Capital DD will return Day in the year. So, 11 April corresponds to 110th day in the year.
yyyy-MM-dd : Small dd will return the Day in the month.
Refer: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use this code :
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(c.getTime());
The formattedDate contain 2016-04-19
Edit :
In your code change the YYYY to yyyy and DD to dd.

How can I extract date from since time component [duplicate]

This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 7 years ago.
I have to extract date in the format MM-dd-yyyy in java from the since time value. Since time is the time at which the doucment is created. For example, if since time is 1452413972759, date would be "Sun, 10 Jan 2016 08:19:32 GMT" (Calculated from http://www.epochconverter.com/) . From this, I could get date in desired format but I am unable to code for the first step i.e., converting since time to date. Can someone help me?
I tried
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date date = df.parse("1452320105343");
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
But it gives parse exception.
Your code can't work because you're trying to parse a number of milliseconds as if it was a MM/dd/yyyy formatted date.
I would have expected the following code to work, where S represents milliseconds :
DateFormat df = new SimpleDateFormat("S");
java.util.Date date = df.parse("1452413972759");
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
However it doesn't for some reason, displaying a date in the 1970 year.
Instead, the following code that parses seconds rather than milliseconds works for your needs :
DateFormat df = new SimpleDateFormat("s");
java.util.Date date = df.parse("1452413972"); // just remove the last 3 digits, or divide by 1000
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
Or just follow the link from #mohammedkhan and use Date constructor rather than parsing a string :
java.util.Date date = new Date(1452413972759l);
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));

SimpleDateFormat Always returning 12.30 AM [duplicate]

This question already has answers here:
Simple date formatting giving wrong time
(4 answers)
Closed 7 years ago.
I am trying to utilise the Calendar apart from implementing my own logic.
I am setting the Calendar value and trying to get the time in a format, below is the code
String timeValue = "06/11/2015 06:30 pm";
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy HH:mm a");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(sdf.parse(timeValue));
Logger.d(TAG, "Hour is = " + calendar.get(Calendar.HOUR));
SimpleDateFormat slotTime = new SimpleDateFormat("hh:mma");
SimpleDateFormat slotDate = new SimpleDateFormat(", dd/MM/yy");
Logger.d(TAG, " Date = " + slotDate.format(calendar.getTime()) + " Time is = " + slotTime.format(calendar.getTime()));
}catch (ParseException parseEx){
parseEx.printStackTrace();
}
I am expecting slotTime.format(calendar.getTime())) should return 6.30 PM while it is returning 12.30 AM.
How can I get the desired output which is 6.30 PM , What mistake I am doing
Your code is OK. The mistake is on the datetime mask:
The ".SSS" field is too much. This is only to expect for milliseconds, and, as far as I can see, you do not expect milliseconds in your input string.
The "HH" mask should be "hh" for 1-12 hours format.
Thus, let it be:
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy hh:mm a");
You have to remove milliseconds from your Simple Date Format (SSS).
I get a java.text.ParseException running your code.
Try using a Simple Date Format string of "dd/MM/yyyy HH:mm a"
you have an error with the String in the time format
String timeValue = "06/11/2015 06:30 pm";
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy hh:mm a.SSS");
a.SSS // .SSS is for Millisenconds which is not correct in the String you are trying to parse.
I removed it and worked fine for me.
Take a look at DateFormat.getTimeInstance(), DateFormat.getDateInstance() and DateFormat.getDateTimeInstance() as these methods return a DateFormat which will honor the users local settings (e.g. 12/24 hour system or date formats like 2016/01/01 or 01.01.2016). This is very important if you plan to release your app in multiple languages. Note that these methods also take a int as parameter with which you can style the resulting format (e.g. short format).
See here for more details.
A complete example would llok like this (creates a String like 3:04 PM on devices with English language and 15:04 on devices with e.g. German language):
String s = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()).format(new Date()); // Creates a String like 3:04 PM

Categories

Resources