3

I am getting date value from DB as a long value. I am converting this to string to use parse function. Given below is my code

 Date date1 = new SimpleDateFormat("MM/dd/yyyy").parse(strDate1);

But the app is crashing when this code is executing.it will successfully execute if the

strDate1="12/30/2012".

But i am having this value as "12302012235"(pzudo value).

How can i do this?

edit:

i am saving date value to DB as INTEGER. from DB i am getting this value and converting to string.this is the actual strDate1 value

strDate1="1346524199000"
andro-girl
  • 7,989
  • 22
  • 71
  • 94
  • 3
    What *exactly* does your `long` value represent? What's it storing, and who's storing it? It makes a *big* difference if it's meant to be (say) "milliseconds since the Unix epoch" or some horrible pseudo-text numeric representation. – Jon Skeet Aug 01 '12 at 06:02
  • Can you just strip off the last 3 characters and then use your value? – Aviral Aug 01 '12 at 06:18

7 Answers7

5

Try the following code segment:

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(Long.parseLong(val));         
        Date d = (Date) c.getTime();        
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");       
        String time = format.format(d);//this variable time contains the time in the format of "day/month/year".    
shfshrf
  • 391
  • 2
  • 11
2

Try this,

SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
    Date dateD=new Date();
    dateD.setTime(LongTime);
    date=dateFormat.format(dateD);
Jin
  • 462
  • 1
  • 7
  • 22
2

Java 8, Convert milliseconds long to Date as String by given date format pattern. If you have a long milliseconds and want to convert them into date string at specifies time zone and pattern, then you can use it:-

dateInMs is a long value of DateTime.

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                    .format(Instant.ofEpochMilli(dateInMs).atZone(ZoneId.of("Europe/London")))
Ajay Kumar
  • 4,864
  • 1
  • 41
  • 44
2

java.time

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* .

Using modern date-time API:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        long input = 12302012235L;

        // Get Instant from input
        Instant instant = Instant.ofEpochMilli(input);
        System.out.println(instant);

        // Convert Instant to ZonedDateTime by applying time-zone
        // Change ZoneId as applicable e.g. ZoneId.of("Asia/Dubai")
        ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
        System.out.println(zdt);

        // Format ZonedDateTime as desired
        // Check https://stackoverflow.com/a/65928023/10819573 to learn more about 'u'
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
        String formatted = dtf.format(zdt);
        System.out.println(formatted);

        // If at all, you need java.util.Date
        Date date = Date.from(instant);
    }
}

Output:

1970-05-23T09:13:32.235Z
1970-05-23T10:13:32.235+01:00[Europe/London]
05/23/1970

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

You can try following code:

private Date getGMTDate(long date) {
    SimpleDateFormat dateFormatGmt = new SimpleDateFormat(
            "yyyy-MMM-dd HH:mm:ss");
    dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    SimpleDateFormat dateFormatLocal = new SimpleDateFormat(
            "yyyy-MMM-dd HH:mm:ss");

    Date temp = new Date(date);

    try {
        return dateFormatLocal.parse(dateFormatGmt.format(temp));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return temp;
}  

I hope this will help you.

CSharp
  • 1,573
  • 2
  • 14
  • 25
0

Try this

Date date1 = new SimpleDateFormat("MMddyyyySSS").parse(strDate1);

Hope it will works for 12302012235 , but i assume 235 is millisec.

abbas.aniefa
  • 2,855
  • 21
  • 30
0

i got the answer.actually i wanted to convert the string to date only for comparing the values.since i am getting the value as long i directly used the compareTo function to do this.avoided the conversion of long to string and string to date conversion.thank you all for support.

andro-girl
  • 7,989
  • 22
  • 71
  • 94