[flutter] 플러터에서 날짜를 다른 타임존으로 변환하는 방법

아래는 플러터에서 package:intl을 사용하여 날짜를 다른 타임존으로 변환하는 예제입니다. 이 예시에서는 현재 날짜와 시간을 UTC로 설정하고, 이를 다른 특정 타임존으로 변환하는 방법을 보여줍니다.

import 'package:intl/intl.dart';

void main() {
  DateTime now = DateTime.now().toUtc();
  String formattedDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(now);
  print('UTC: $formattedDate');

  String localTimeZone = 'Asia/Seoul'; // 대한민국 시간대
  DateTime localDateTime = now.toLocal();
  String formattedLocalDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(localDateTime);
  print('$localTimeZone: $formattedLocalDate');
}

이 코드를 실행하면 현재 UTC 시간과 지정한 지역의 시간이 출력됩니다. 위 코드에서는 Asia/Seoul을 예시로 사용했지만, 원하는 타임존에 따라 localTimeZone을 변경할 수 있습니다.

package:intl은 날짜와 시간을 다양한 형식으로 표시하고 변환하는 데 사용되며, 다른 타임존으로의 변환도 가능합니다.

플러터에서 날짜와 시간을 처리하는 데 사용되는 다른 라이브러리나 방법들도 있으므로, 이를 참조하여 필요에 맞게 사용하는 것이 좋습니다.