2017-02-15 6 views
0

hallo iam immer einen Zeitstempel vom Server in gregorianischen was Look gefällt daswie String-Zeitstempel von gregorianischen konvertieren Shamsi

{ 
    "error": false, 
    "users": [ 
    { 
     "id": 1, 
     "username": "dfsg", 
     "active": 1, 
     "created_at": "2017-02-14 00:13:25" 
    }, 
    { 
     "id": 2, 
     "username": "asf", 
     "active": 1, 
     "created_at": "2017-02-14 13:42:26" 
    }, 
    { 
     "id": 3, 
     "username": "test2", 
     "active": 1, 
     "created_at": "2017-02-15 13:57:31" 
    } 
    ] 
} 

, und ich will es ändern Shamsi, was wie dieses 1395- aussehen sollte 11-26 13:42:26 Gibt es irgendeine Klasse, es zu tun?

dank

Antwort

1

Verwenden Joda Time

Als solche:

 Chronology iso = ISOChronology.getInstanceUTC(); 
     Chronology hijri = IslamicChronology.getInstanceUTC(); 

     LocalDate todayIso = new LocalDate(2013, 3, 31, iso); 
     LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(), 
              hijri); 
     System.out.println(todayHijri); // 1434-05-19 

Um das Jahr, Monat und Tag zu erhalten aus der Zeichenfolge die folgende verwenden:

String startDateString = "2017-02-14 00:13:25"; 
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
Date date = df.parse(startDateString); 
Calendar calendar = new GregorianCalendar(); 
calendar.setTime(date); 

int year = calendar.get(Calendar.YEAR); 
//Add one to month {0 - 11} 
int month = calendar.get(Calendar.MONTH) + 1; 
int day = calendar.get(Calendar.DAY_OF_MONTH); 
+0

ty @ zed aber es ist für Hijri und ich brauche Shamsi Conversion –

+0

Aah entschuldige mich, ich las https://en.wikipedia.org/wiki/Solar_Hijri_calendar und dachte Shamsi ist Hijri. Sind Sie sicher, dass es nicht das Gleiche ist? – zed

+0

ja sir hijri ist arabisch und shamsi ist für persians danke im voraus –

1

Sie können diese Klasse hinzufügen in Ihrem Projekt, source

import java.text.SimpleDateFormat; 
import java.util.Date; 

public class PersianDate { 
    public String todayShamsi() 
    { 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
     String curentDateandTime = sdf.format(new Date()); 
     String year = curentDateandTime.substring(0, 4); 
     String month = curentDateandTime.substring(4, 6); 
     String day = curentDateandTime.substring(6, 8); 
     int Y = Integer.valueOf(year); 
     int M = Integer.valueOf(month); 
     int D = Integer.valueOf(day); 
     return Shamsi(Y, M, D); 
    } 
    public static String Shamsi(int Y, int M, int D) 
    { 
     if (Y == 0) 
      Y = 2000; 
     if (Y < 100) 
      Y = Y + 1900; 
     if (Y == 2000) 
     { 
      if (M > 2) 
      { 
       SimpleDateFormat temp = new SimpleDateFormat("yyyyMMdd"); 
       String curentDateandTime = temp.format(new Date()); 
       String year = curentDateandTime.substring(0, 4); 
       String month = curentDateandTime.substring(4, 6); 
       String day = curentDateandTime.substring(6, 8); 
       Y = Integer.valueOf(year); 
       M = Integer.valueOf(month); 
       D = Integer.valueOf(day); 
      } 
     } 
     if (M < 3 || (M == 3 && D < 21)) 
     { 
      Y -= 622; 
     } 
     else Y -= 621; 
     switch (M) 
     { 
     case 1: if (D < 21) 
     { 
      M = 10; 
      D = D + 10; 
     } 
     else 
     { 
      M = 11; 
      D -= 20; 
     } 
     break; 
     case 2: if (D < 20) 
     { 
      M = 11; 
      D = D + 11; 
     } 
     else 
     { 
      M = 12; 
      D -= 19; 
     } 
     break; 
     case 3: 
      if (D < 21) 
      { 
       M = 12; 
       D = D + 9; 
      } 
      else 
      { 
       M = 1; 
       D -= 20; 
      } 
      break; 
     case 4: 
      if (D < 21) 
      { 
       M = 1; 
       D = D + 11; 
      } 
      else 
      { 
       M = 2; D = D - 20; 
      } 
      break; 
     case 5: 
      if (D < 22) 
      { 
       M = M - 3; 
       D = D + 10; 
      } 
      else 
      { 
       M = M - 2; 
       D = D - 21; 
      } 
      break; 
     case 6: 
      if (D < 22) 
      { 
       M = M - 3; 
       D = D + 10; 
      } 
      else 
      { 
       M = M - 2; 
       D = D - 21; 
      } 
      break; 
     case 7: 
      if (D < 23) 
      { 
       M = M - 3; 
       D = D + 9; 
      } 
      else 
      { 
       M = M - 2; 
       D = D - 22; 
      } 
      break; 
     case 8: 
      if (D < 23) 
      { 
       M = M - 3; 
       D = D + 9; 
      } 
      else 
      { 
       M = M - 2; 
       D = D - 22; 
      } 
      break; 
     case 9: 
      if (D < 23) 
      { 
       M = M - 3; 
       D = D + 9; 
      } 
      else 
      { 
       M = M - 2; 
       D = D - 22; 
      } 
      break; 
     case 10: 
      if (D < 23) 
      { 
       M = 7; 
       D = D + 8; 
      } 
      else 
      { 
       M = 8; 
       D = D - 22; 
      } 
      break; 
     case 11: 
      if (D < 22) 
      { 
       M = M - 3; 
       D = D + 9; 
      } 
      else 
      { 
       M = M - 2; 
       D = D - 21; 
      } 
      break; 
     case 12: 
      if (D < 22) 
      { 
       M = M - 3; 
       D = D + 9; 
      } 
      else 
      { 
       M = M - 2; 
       D = D - 21; 
      } 
      break; 
     } 
     String month = "00"; 
     String day = "00"; 
     //D = Integer.valueOf(D)+1; 
     if (M < 10) 
     { 
      month = "0" + M; 
     } 
     else 
     { 
      month = String.valueOf(M); 
     } 
     if (D < 10) 
     { 
      day = "0" + D; 
     } 
     else 
     { 
      day = String.valueOf(D); 
     } 
     return String.valueOf(Y) + "/" + month + "/" + day; 
    } 
} 
+0

Dank bro seine Notwendigkeit 3 ​​ganzzahligen Wert als Jahr und Motte und Tag Reams Shamsi. Wie kann ich diese Werte von meiner Zeichenkette erhalten? –

Verwandte Themen