How to get clock from a date [duplicate] - java

This question already has answers here:
How to format date and time in Android?
(26 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I have Date like this Sun May 20 09:18:44 GMT+04:30 2018 how i can get clock from this Date,
i want result like this 09:18 am
Log.v(TAG,"date "+ date.toString());// output is Sun May 20 09:18:44 GMT+04:30 2018
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);//NullPointerException

your code is working you just have to initialize Date class's object
Date date = new Date();
Date date = new Date();
Log.v(TAG,"date "+ date.toString());
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);
Log.v(TAG,"date "+ time);

Try this code..
SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
SimpleDateFormat sdf4 = new SimpleDateFormat("hh:mm", Locale.ENGLISH);
Date d1 = null;
try{
d1 = sdf3.parse("Sun May 20 09:18:44 GMT+04:30 2018");
String date=sdf4.format(d1);
System.out.println("time..." + date);
}catch (Exception e){ e.printStackTrace(); }
System.out.println("check..." + d1);

Related

ParseException: Unparseable date in Java [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 3 years ago.
I need to convert String (in date format) to TimeStamp.
IN first I did like this.
private Timestamp dateConverter(String date) {
Timestamp timestamp = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(date);
timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
Result was: ParseException: Unparseable date: "16 Jan 2020 11:02"
I tried by another way :
private Timestamp dateConverter(String date) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm");
LocalDateTime ldt = LocalDateTime.parse(date , f);
return Timestamp.valueOf(ldt);
}
At this time I have Internal Server Error with DateTimeParseException: Text '16 Jan 2020 11:02' could not be parsed at index 3
Your pattern can't know what is the language of your month, English, French, Arabic, Russian, Mandarin..., to solve this you have to help the formatter by using Locale like so :
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm")
.withLocale(new Locale("us"));
LocalDateTime ldt = LocalDateTime.parse("16 Jan 2020 11:02" , f); // 2020-01-16T11:02

convert string to date with locale [duplicate]

This question already has answers here:
Changing String date format
(4 answers)
Closed 4 years ago.
I'm trying to convert a string to date but every time i do it keeps throwing errors at me, Im not sure what i am missing
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
// m brings in all variables from a get/setter
date = format.parse(m.getEventTime());
} catch (ParseException e) {
e.printStackTrace();
Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(date.toString());
My variable m.getEventTime() is passing the following string 2018-04-28 14:00:00
I have tried passing the string as 2018-04-28T14:00:00Z to no avail.
No errors are coming from the stack trace from the try/catch block but the log is printing out D/Response.Error.Date: 2017-08-19 15:00:00
when i add e.toString() to the log it prints out D/Response.Error.Date: 2017-08-19 15:00:00 java.text.ParseException: Unparseable date: "2017-08-19 15:00:00"
On the actual application when run the time is shown as now Sat Apr 28 08:22:33 GMT+01:00 2018
Am i missing something?
You can try this,
Date date = new Date();
SimpleDateFormat oldformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
// m brings in all variables from a get/setter
date = oldformat.parse(m.getEventTime());
} catch (ParseException e) {
e.printStackTrace();
Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(format.format(date));
You can get time format like this:
String dateTime = null;
DateFormat df = new SimpleDateFormat("dd-MM-yyyy, hh:mm a");
//here you can use your required format
dateTime = df.format(new Date(stringTime));
You are converting date into English which is causing the exception:
Please Change:
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
eventTime.setText(date.toString());
with
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a");
eventTime.setText(format.format(date));

SimpleDateFormat Java dates [duplicate]

This question already has answers here:
Java SimpleDateFormat Parse Wrong Date
(2 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 5 years ago.
Any idea what is happening to this code that the dates do not coincide? First prints "20 December 2017" and then "Date with new format:2017-01-01"...
SimpleDateFormat formatter = new SimpleDateFormat("d MMMM YYYY");
SimpleDateFormat formatterNew = new SimpleDateFormat("yyyy-MM-dd");
try{
System.out.println(formatter.format(new Date()));
Date date = formatter.parse("20 December 2017");
System.out.println("Date with new format:" +formatterNew.format(date));
}catch(Exception ex){
ex.printStackTrace();
}
This is related to the use of yyyy vs YYYY. If you change your code to use yyyy it will work.
Here is the explanation of the difference between the two.
Week year

How to count down 6 month in android? [duplicate]

This question already has answers here:
android java parse date from string
(3 answers)
Closed 5 years ago.
I have a date string in this format '20161201'.
How to make this a datetime string to be july 2016?
I want to show 6 months before that datetime string.
Is it possible?
Also how do I convert just '20161201' to Dec 2016?
you change the format using the following code,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
SimpleDateFormat expectedSDF = new SimpleDateFormat("MMMM yyyy", Locale.getDefault());
Date date = simpleDateFormat.parse(dateString);
String newFormatString = expectedSDF.format(date);
And for the countdown, use CountDownTimer
https://developer.android.com/reference/android/os/CountDownTimer.html
For convert '20161201' to 'Dec 2016':
//Convert string to date
String dateString = "20161201";
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyyMMdd");
Date convertedDate = new Date();
try {
convertedDate = parseDateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
//Convert date to string
SimpleDateFormat outputDateFormat = new SimpleDateFormat("MMM yyyy");
String result = outputDateFormat.format(convertedDate);
//Print the result
System.out.println(result);
In '6 months' to millisecond is 15778476000. You can get the date after 6 months in millisecond by using
long resultdate = convertedDate.getTime() - 15778476000L;
and then
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(resultdate);
String result = outputDateFormat.format(calendar.getTime());
to get the result
https://developer.android.com/reference/java/text/SimpleDateFormat.html
https://developer.android.com/reference/java/util/Date.html
https://developer.android.com/reference/java/util/Calendar.html#setTimeInMillis(long)
how to make this datetime string to be july 2016
I want to show 6 months before that datetime string
July is 5 months...
Here's the 6 month answer
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(new SimpleDateFormat("yyyyMMdd").parse("20161201"));
c.set(Calendar.MONTH, c.get(Calendar.MONTH)-6);
String output = new SimpleDateFormat("MMMM yyyy").format(c.getTime()));
// June 2016

Convert string to a formated date in java [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 9 years ago.
Hi, I have a string like:
String selectedDate = "Fri Jan 17 00:00:00 CAT 2014";
I need to format it to:
DD.MM.YYYY = 17.01.2014 00:00:00
Can anyone help please see code below:
String date = getDate(selectedDate);
private String getDate(String inpuDate) {
Date date = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss ").parse(inputDate);
return new SimpleDateFormat("dd.mm.yyyy hh:mm:ss").format(date);
}
Try the following:
String date = getDate(selectedDate);
private String getDate(String inpuDate) {
Date date = new SimpleDateFormat("EEE MMM d hh:mm:ss z yyyy").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy hh:mm:ss").format(date);
}

Categories

Resources