I want this method to convert and get value in miliseconds [duplicate] - java

This question already has answers here:
How to convert a date to milliseconds
(5 answers)
Closed 3 years ago.
How can I do that? I tried a lot but it is not working properly.
public static String getDateTime() {
String dateStr = "";
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateStr = dateFormat.format(date);
return dateStr;
}
Please someone right another method which return miliseconds from above Date format. Of course! In my DB I'm storing this value: 2019-04-17 17:11:02 -> I want this value to convert in miliseconds using one method. so that I call new method and pass that variable value.

public static long milisecondsFromDate(String dateStr) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = formatter.parse(dateStr);
return date.getTime();
} catch (Exception e) {
Log.e("Tag", "Wrong date Format");
}
return -1;
}

I would strongly suggest to use Java8 Date/Time API for your cause, something like:
public static void main(String[] args) {
System.out.println(getDateTime());
}
public static String getDateTime() {
String dateStr = "2019-04-17 17:11:02";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter);
return String.valueOf(localDateTime.toInstant(ZoneOffset.UTC).getEpochSecond());
}

try this:-
long timeInMilliseconds = 0;
String givenDateString = "2019-04-17 17:11:02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
String timeInMilliseconds = String.valueOf(timeInMilliseconds);

Related

How to add one hour to UTC in android

I'm trying get current time and then, convert to UTC+1 time zone which is in yyyy-MM-dd HH:mm:ss format
I tried the following code. It didn't work. Can anyone help?
public static String getDateTime() {
Date time = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC+1"));
return outputFmt.format(time);
}
I think you can try:
public static String getDateTime() {
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Read current time already in desired timezone
Calendar time = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
return outputFmt.format(time.getTime());
}
Following method will add one hour to UTC time.
public static String getDateTime()
{
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
String utctime=outputFmt.format(new Date());
String time=null;
Calendar calendar=null;
Date date;
try
{
calendar = Calendar.getInstance();
calendar.setTime(outputFmt.parse(utctime));
calendar.add(Calendar.HOUR, 1);
date=calendar.getTime();
time=outputFmt.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}

Android: how to get the timezone string in the format specified here

How can I obtain the timezone format and return the string like so
https://msdn.microsoft.com/en-us/library/gg154758.aspx - column Time zone name,
for example "SA Pacific Standard Time"
or worse comes to worst, how can I obtain it like so: (UTC-05:00) Bogota, Lima, Quito, or at least (UTC-05:00)
so that I can manually match it to the former String if I put them all in a map?
Have you tried?
TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());
Result be if your device happens to be in Australia
TimeZone GMT+09:30 Timezon id :: Australia/Darwin
This should return UTC as a String and Date object. You can change the date format.
static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"
public static Date getUTCdatetimeAsDate()
{
//note: doesn't check for null
return stringDateToDate(GetUTCdatetimeAsString());
}
public static String getUTCdatetimeAsString()
{
final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());
return utcTime;
}
public static Date stringDateToDate(String StrDate)
{
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
try
{
dateToReturn = (Date)dateFormat.parse(StrDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
return dateToReturn;
}
This might be irrelevant (don't know if Android fully supports java-8), but you can do it using standard java api:
ZonedDateTime dateTime = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Asia/Tokyo]");
System.out.println(dateTime); // 2007-12-03T10:15:30+09:00[Asia/Tokyo]
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("(O) z");
System.out.println(dateTime.format(formatter)); // (GMT+9) JST

how to convert mm/dd/yyyy to yyyy-mm-dd in java [duplicate]

This question already has answers here:
How to convert string "2011-11-29 12:34:25" to date in "dd-MM-yyyy" format in JAVA
(6 answers)
Closed 8 years ago.
i am getting input date as String into mm/dd/yyyy and want to convert it into yyyy-mm-dd
i try out this code
Date Dob = new SimpleDateFormat("yyyy-mm-dd").parse(request.getParameter("dtDOB"));
OK - you've fallen for one of the most common traps with java date formats:
mm is minutes
MM is months
You have parsed months as minutes. Instead, change the pattern to:
Date dob = new SimpleDateFormat("yyyy-MM-dd").parse(...);
Then to output, again make sure you use MM for months.
String str = new SimpleDateFormat("dd-MM-yyyy").format(dob);
It should be
SimpleDateFormat("yyyy-MM-dd")
capital M
For More info refer Oracle Docs
As alternative to parsing you can use regex
s = s.replaceAll("(\\d+)/(\\d+)/(\\d+)", "$3-$2-$1");
Ex -
String dob = "05/02/1989"; //its in MM/dd/yyyy
String newDate = null;
Date dtDob = new Date(dob);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
newDate = sdf.format(dtDob);
} catch (ParseException e) {}
System.out.println(newDate); //Output is 1989-05-02
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FormatDate {
private SimpleDateFormat inSDF = new SimpleDateFormat("mm/dd/yyyy");
private SimpleDateFormat outSDF = new SimpleDateFormat("yyyy-mm-dd");
public String formatDate(String inDate) {
String outDate = "";
if (inDate != null) {
try {
Date date = inSDF.parse(inDate);
outDate = outSDF.format(date);
} catch (ParseException ex)
System.out.println("Unable to format date: " + inDate + e.getMessage());
e.printStackTrace();
}
}
return outDate;
}
public static void main(String[] args) {
FormatDate fd = new FormatDate();
System.out.println(fd.formatDate("12/10/2013"));
}
}

How do I parse "dd.MM.yyyy G" to ISO-Date in Java? [duplicate]

This question already has answers here:
How to parse date string to Date? [duplicate]
(6 answers)
Closed 9 years ago.
Hey guys I have the date "01.01.1000 AD"(SimpleDate) as String and dd.MM.yyyy G(SimpleFormat) and need to parse it into a Standard ISO-Date in the form 1995-12-31T23:59:59Z (yyyy-MM-dd'T'hh:mm:ss'Z')
my actual code is:
public static String getISODate(String simpleDate, String simpleFormat, String isoFormat) throws ParseException {
Date date;
if (simpleFormat.equals("long")) {
date = new Date(Long.parseLong(simpleDate));
} else {
SimpleDateFormat df = new SimpleDateFormat(simpleFormat);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
// or else testcase
// "1964-02-24" would
// result "1964-02-23"
date = df.parse(simpleDate);
}
return getISODate(date, isoFormat);
}
Does anyone have an idea how do I do that?
Try this:
String string = "01.01.1000 AD";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy GG");
Date date = dateFormat.parse(string);
The G in the date format stands for era.
See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
This will hopefully help [tricky with standard jdk, but at least possible - and JSR 310 doesn't support this feature :-( ]:
DateFormat df = new SimpleDateFormat("dd.MM.yyyy GG", Locale.US);
DateFormat iso = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = df.parse("01.01.1000 AD");
System.out.println(iso.format(d)); // year-of-era => 1000-01-01 (not iso!!!)
// now let us configure gregorian/julian date change right for ISO-8601
GregorianCalendar isoCalendar = new GregorianCalendar();
isoCalendar.setGregorianChange(new Date(Long.MIN_VALUE));
iso.setCalendar(isoCalendar);
System.out.println(iso.format(d)); // proleptic iso year: 1000-01-06
} catch (ParseException ex) {
ex.printStackTrace();
}
Something like this?
String date = "01.01.1000 AD";
SimpleDateFormat parserSDF = new SimpleDateFormat("dd.mm.yyyy GG");
System.out.println(parserSDF.parse(date));
Try it may be help:
public static String getISODate(String simpleDate, String simpleFormat, String isoFormat) throws ParseException {
Date date;
if (simpleFormat.equals("long")) {
date = new Date(Long.parseLong(simpleDate));
} else {
SimpleDateFormat df = new SimpleDateFormat(simpleFormat);
df.setTimeZone(TimeZone.getTimeZone("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
// or else testcase
// "1964-02-24" would
// result "1964-02-23"
date = df.parse(simpleDate);
}
return getISODate(date, isoFormat);
}

how to convert long date value to mm/dd/yyyy format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
converting long string to date
I need to convert long date value to mm/dd/yyyy format.my long value is
strDate1="1346524199000"
please help me
Refer Below code which give the date in String form.
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test{
public static void main(String[] args) {
long val = 1346524199000l;
Date date=new Date(val);
SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yy");
String dateText = df2.format(date);
System.out.println(dateText);
}
}
Refer below code for formatting date
long strDate1 = 1346524199000;
Date date = new Date(strDate1);
try {
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yy");
date = df2.format(format.parse("yourdate");
} catch (java.text.ParseException e) {
e.printStackTrace();
}
Try this example
String[] formats = new String[] {
"yyyy-MM-dd",
"yyyy-MM-dd HH:mm",
"yyyy-MM-dd HH:mmZ",
"yyyy-MM-dd HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
};
for (String format : formats) {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
}
and read this http://developer.android.com/reference/java/text/SimpleDateFormat.html
Try something like this:
public class test
{
public static void main(String a[])
{
long tmp = 1346524199000;
Date d = new Date(tmp);
System.out.println(d);
}
}

Categories

Resources