convert milliseconds to date [duplicate] - java

This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 8 years ago.
Server sending me time as 1390361405210+0530 so if I want to convert this in to date then should I have to add 0530 into 1390361405210 and then calculate date and time?
Any suggestion should be appreciated.Thanks

How about this.
long currentDateTime = 1390361405210L;
Date currentDate = new Date(currentDateTime);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss Z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+530"));
System.out.println(sdf.format(currentDate));

public static void main( String[] args )
{
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
long milliSeconds=1390361405210L;
Date date = new Date(milliSeconds);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));
System.out.println(formatter.format(date));
}

If we consider that the first part of the String is the number of milliseconds since the epoch, and the second part is a timezone indication (in that case, IST, Indian Standard Time), you can get a readable date like this :
final String jsonDate = "1390361405210+0530";
final Date date = new Date(Long.parseLong(jsonDate.substring(0, jsonDate.length() - 5)));
final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT" + jsonDate.substring(jsonDate.length() - 5)));
System.out.println(format.format(date));
Output:
January 22, 2014 9:00:05 AM GMT+05:30

Related

How to convert into dd-mmm-yyyy format to dd-mm-yyyy format in swing(core java)? [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
How can I change the date format in Java? [duplicate]
(10 answers)
ParseException when parsing 3 character abbreviated month using SimpleDateFormat
(5 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 3 years ago.
To convert dd-mmm-yyyy to dd-mm-yyyy format.
String dateofbirth = ((JTextField) dobcalender.getDateEditor().getUiComponent()).getText();//date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
tried these codes :
System.out.println(myFormat.format(myFormat.parse(dateofbirth)));
myFormat.format(myFormat.parse(dateofbirth));
showing error parseexception
Based on the format "24 Feb 2019"
SimpleDateFormat from=new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat to=new SimpleDateFormat("dd-MM-yyyy");
Date frm=from.parse("24 Feb 2019");
System.out.println(frm);//Sun Feb 24 00:00:00 IST 2019
System.out.println(to.format(frm));//24-02-2019
First, you need to parse the String to Date object. Then you need to convert the Date object to a new formatted String. Here is the sample code:
String dateofbirth = "09-10-2010"; //date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
// converting to Date object
Date date = myFormat.parse(dateofbirth);
SimpleDateFormat myFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
// converting Date object to new format
String formattedDate = myFormat1.format(date);
System.out.println(formattedDate); // prints: 09-Oct-2010
// Before Java8 , No thread safe , has to import multiple packages(util, text), throws checked Exception(ParseException)
System.out.println(oldDate); //21 Jul 2019
SimpleDateFormat oldPattern = new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat newPattern = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = oldPattern.parse(oldDate);
String newDate = newPattern.format(date);
System.out.println(newDate); //21-07-2019
} catch (ParseException e) {
// Exception handling message/mechanism/logging as per company standard
}
// In Java8, Thread Safe, Immutable, throws unchecked Exception(DateTimeParseException)
DateTimeFormatter oldPattern8 = DateTimeFormatter.ofPattern("dd MMM-yyyy");
DateTimeFormatter newPattern8 = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate datetime = LocalDate.parse(oldDate, oldPattern8);
String output = datetime.format(newPattern8);
System.out.println(output); //21-07-2019

Date conversion in MM/DD/YY h:mm:ss a format in java [duplicate]

This question already has answers here:
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
Java string to date conversion
(17 answers)
Calendar.getInstance gives wrong date [duplicate]
(2 answers)
Closed 4 years ago.
I am trying to get this date 09/03/18 6:30:00 PM using 4-SEP-2018 but I am getting 12/364/17 6:30:00 PM, Here is what I have tried.
public class StockBuySell
{
static String DATE_FORMAT_UI = "DD-MMM-YYYY";
public static void main(String args[]) throws Exception {
DateFormat outputFormat = new SimpleDateFormat("MM/DD/YY h:mm:ss a");
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
DateFormat inputFormat = new SimpleDateFormat(DATE_FORMAT_UI);
String inputText = "4-SEP-2018";
Date date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);
System.out.println(outputText);
}
}
As per the JavaDoc, D stands for Day in year. You will need to replace DD with dd (day in month).
Y stands for Week year, not year.
In short, this: "DD-MMM-YYYY" needs to be this: "dd-MMM-yyyy".
Try this:
String DATE_FORMAT_UI = "dd-MMM-yyyy";
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yy h:mm:ss a");
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
DateFormat inputFormat = new SimpleDateFormat(DATE_FORMAT_UI);
String inputText = "4-SEP-2018";
Date date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);
System.out.println(outputText);
You are going to have time zone issues as you're forcing the GMT... the date you're rendering will be in the local time zone so it may change the date.

How to convert this Date string into a long? [duplicate]

This question already has answers here:
Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java [duplicate]
(2 answers)
Closed 6 years ago.
I am having issues converting a String that has this format for date from the server into a long?
Example Date String - "2016-07-04T00:02:34.457Z" (Note this is a string)
I tried this below but needs try catch around gmt, when I add it and a not null around cmtDt - then I initialize cmtDt to 0 pre setting it on the bottom and it is always 0.
Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
formatter.setTimeZone(tz);
Date gmt = formatter.parse(comment.getDateCommented());
cmtDt = gmt.getTime();
First, your input String includes milliseconds (and your format does not). Second, your input String includes a literal Z (which is presumably to indicate a UTC timezone). Finally, getting your system timezone and assigning it to the formatter isn't reliably going to be UTC. You need something like,
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
long cmtDt = gmt.getTime();
System.out.println(cmtDt);
} catch (Exception e) {
e.printStackTrace();
}
Which I ran, and got
1467590554457
Your format string for the SimpleDateFormat needs to be:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
My test code that works is:
Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
formatter.setTimeZone(tz);
Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
long cmtDt = gmt.getTime();
System.out.println("cmtDt = " + cmtDt);
The format in SDF needs to be fixed. The following will help you.
Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
formatter.setTimeZone(tz);
Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
long cmtDt = gmt.getTime();
System.out.println(cmtDt);
Prints : 1467599640457

YYYY-MM-DD minus 1 day [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 9 years ago.
I have a date variable in the YYYY-MM-DD format.
How can I change the date value to the previous day? So if the value of the variable was 2014-01-01 it would change to 2014-12-31.
You can use a DateFormat and a Calendar, like so
String fmt = "yyyy-MM-dd";
String dt = "2014-01-01";
java.text.DateFormat df = new java.text.SimpleDateFormat(fmt);
java.util.Calendar cal = java.util.Calendar.getInstance();
try {
cal.setTime(df.parse(dt));
cal.add(java.util.Calendar.DAY_OF_MONTH, -1);
System.out.println(cal.getTime());
} catch (Exception e) {
}
Which outputs
Tue Dec 31 00:00:00 EST 2013
Java can parse a date, then subtract one day and output the toString()
documentation: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html
Long version:
String example = "2014-01-01";
DateFormat df = new SimpleDateFormat("YYY-MM-dd", Locale.ENGLISH);
Date result = df.parse(target);
Calendar cal = Calendar.getInstance();
cal.setTime(result);
cal.add(Calendar.DATE, -1);
result = cal.getTime();
System.out.println(df.format(result));

Format current date to show day of the week [duplicate]

This question already has answers here:
How to determine day of week by passing specific date?
(28 answers)
Closed 6 years ago.
I want to show the current date in my application like this:
Thu, May 2, 2013
I already have the following code to get the current date
Calendar c = Calendar.getInstance();
Time time = new Time();
time.set(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH),
c.get(Calendar.YEAR));
How can I format this Time object to the string I need?
This does what you want
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: " + strDate);
Where strDate can be displayed in your textView or whatever
Maybe you can use it.
This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class.
import java.text.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Date dt = new Date(1000000000000L);
DateFormat[] dtformat = new DateFormat[6];
dtformat[0] = DateFormat.getInstance();
dtformat[1] = DateFormat.getDateInstance();
dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
for(DateFormat dateform : dtformat)
System.out.println(dateform.format(dt));
}
}
output:
9/9/01 7:16 AM
Sep 9, 2001
Sep 9, 2001
Sunday, September 9, 2001
September 9, 2001
9/9/01
Source
Use this
SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM dd,yyyy");
String formattedDate = formatter.format(new Date(System.currentTimeMillis()));
Log.e("formattedDate",formattedDate);
I will suggest to use java.text.SimpleDateFormat instead.
public static void main(String[] args) {
Date date=new Date();
String format = new SimpleDateFormat("EEE,MMM d,yyyy ").format(date);
System.out.println(format);
}
SimpleDateFormat dateformat= new SimpleDateFormat("dd,MM,yyyy");
String strdate = dateformat.format(new Date(System.currentTimeMillis()));
Oops, I'm a bit slow.

Categories